Last post Jul 08, 2014 06:11 AM by AidyF
Member
7 Points
206 Posts
Jul 08, 2014 05:56 AM|GStar99|LINK
I have a string variable that represents the name of a child class. Example:
string s = "CustomerA"
I will need to create a customer using the base class. So, the syntax needed is:
Customer cust = new ..
what is the sytax?
I've had a look at the activator.create instance method but I cant really figure out the syntax
Customer cust = (Customer)Activator.CreateInstance(Type.GetType("CustomerA"));
Using the above gives me the following error
Value cannot be null. Parameter name: type
All-Star
37441 Points
9076 Posts
Jul 08, 2014 06:11 AM|AidyF|LINK
Your code is fine, however you need to provide the full type name, not just the class name.
string s = "YourNamespace.CustomerA";
Member
7 Points
206 Posts
How can you instanitiate a class using a string value?
Jul 08, 2014 05:56 AM|GStar99|LINK
I have a string variable that represents the name of a child class. Example:
I will need to create a customer using the base class. So, the syntax needed is:
what is the sytax?
I've had a look at the activator.create instance method but I cant really figure out the syntax
Using the above gives me the following error
All-Star
37441 Points
9076 Posts
Re: How can you instanitiate a class using a string value?
Jul 08, 2014 06:11 AM|AidyF|LINK
Your code is fine, however you need to provide the full type name, not just the class name.
string s = "YourNamespace.CustomerA";