how to initiate a class stored in string variable
-
I know its a silly ques. I've got treeview control which holds different class names. In normal ways what we do is.
Ayclass cls= new AnyClass();
now the AnyClass's name is stored in a variable. how to instantiate a class. here's the failed code.
string varClass= treeview1.node.ToString();
varclass d= new varClass();Hope I make myself clarify.
-
I know its a silly ques. I've got treeview control which holds different class names. In normal ways what we do is.
Ayclass cls= new AnyClass();
now the AnyClass's name is stored in a variable. how to instantiate a class. here's the failed code.
string varClass= treeview1.node.ToString();
varclass d= new varClass();Hope I make myself clarify.
Normally you don't do this at all. It's possible using reflection, but having to use reflection is usually an indication that you are doing something backwards. What is it that you are trying to accomplish, really? You should ask for a solution for that, instead of asking about the way that you think that it's solved.
--- single minded; short sighted; long gone;
-
I know its a silly ques. I've got treeview control which holds different class names. In normal ways what we do is.
Ayclass cls= new AnyClass();
now the AnyClass's name is stored in a variable. how to instantiate a class. here's the failed code.
string varClass= treeview1.node.ToString();
varclass d= new varClass();Hope I make myself clarify.
If you are sure that's the way to go (see post from Guffa) then this is how it's working: object d = Activator.CreateInstance(Type.GetType(typeName)); see help on CreateInstance for further information.
-^-^-^-^-^- no risk no funk
-
If you are sure that's the way to go (see post from Guffa) then this is how it's working: object d = Activator.CreateInstance(Type.GetType(typeName)); see help on CreateInstance for further information.
-^-^-^-^-^- no risk no funk
here's what I want. here's chart reports in treeview control for end user to display. Sales_Monthwise Sales_Citywise sales_PartyWise when a user clicks Sales_Monthwise I want open Charting Class with the same name. one possible way is:
if (treeview.node.text == "Sales_MonthWise) {
Sales_Monthwise sm= new Sales_Monthwise();
sm.Show();
}bcoz there are so many classes I don't want to insert logic for every class.
-
here's what I want. here's chart reports in treeview control for end user to display. Sales_Monthwise Sales_Citywise sales_PartyWise when a user clicks Sales_Monthwise I want open Charting Class with the same name. one possible way is:
if (treeview.node.text == "Sales_MonthWise) {
Sales_Monthwise sm= new Sales_Monthwise();
sm.Show();
}bcoz there are so many classes I don't want to insert logic for every class.