I am in need of some help here about doing a dynamic instantiation in C#. What I want to accomplish is to be able to use a string variable that is used as the name in the instantiation. I think you can use reflection or something, but I am lost on this one. Here is my test code snippet and hopefully someone has an answer.
Averages is tied to a class that handles everything. So lets say I wanted to make test the variable and everything that is tied to the string of test could be passed as the instantiation. How could I create an object that can handle the variable test coming in, compile and be used in runtime? I know this may sound out of the ordinary, but instead of me using many IF's with multiple declarations of doubles. I could use a dynamic instantiation. Anyone that can help out I would be most appreciative.
Averages test = new Averages(); double[] testresult; testresult = test.sma(); womp,,,I want to dynamically declare arrays of doubles. I already know how to declare a static array. What I am trying to accomplish is eliminating declaring 30 arrays that bascially do the same thing over and over again with a different naming.
So instead of doing this:
if (UITAName == "SMA") { Averages sma = new Averages(); double[] smaresult; smaresult = sma.sma(UITAName, YVal, UITPeriod, UITShift); chart1.Series[UITA].Points.DataBindXY(test2, test1); } if (UITAName == "TMA") { Averages tma = new Averages(); double[] tmaresult; tmaresult = tma.tma(UITAName, YVal, UITPeriod); chart1.Series[UITA].Points.DataBindXY(XVal, tmaresult); } else if (UITAName == "EMA") { Averages ema = new Averages(); double[] emaresult; emaresult = ema.ema(UITAName, YVal, UITPeriod); chart1.Series[UITA].Points.DataBindXY(XVal, emaresult); } I want to do this only once for everything instead of doing IF statements. The problem is that you cannot compile with a declaration of a string. There has to be a way I just do not know how.
Averages UITAName = new Averages(); double[] UITANameresult; UITANameresult = UITAName.UITAName(UITAName, YVal, UITPeriod); chart1.Series[UITA].Points.DataBindXY(XVal, UITANameresult);