Dynamically selecting Class for use in program. | Member | | Join Date: Sep 2009 Location: Usa, Michigan, Westland
Posts: 49
| |
I have 10 Classes that just hold data. int and string data. Nothing fancy.
Yet. I have 10 possible selections from the user.
Based on the selection. I want to Instantiate a specific class.
Ver is the namespace the Data Classes are within.
I don't have an Interface for this, and am not sure if I should. All 10 classes will have the exact same name for the variables. Just different internal data.
I'll be running a huge loop (132 times) for this. So I would rather have this Dynamic method of declaring CurrentV so I don't have to do 10 repeats of the same code. - switch (ClassID) {
-
case 0: Ver.Data1 CurrentV = new Ver.Data1(); break;
-
case 1: Ver.Data2 CurrentV = new Ver.Data2(); break;
-
case 2: Ver.Data3 CurrentV = new Ver.Data3(); break;
-
}
This doesn't work... Any ideas?
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,783
| | | re: Dynamically selecting Class for use in program.
So all of these 10 classes are derived from the same base class? That way they inherit all of these identical properties?
| | Familiar Sight | | Join Date: Jul 2009 Location: Calgary, Alberta, Canada
Posts: 233
| | | re: Dynamically selecting Class for use in program.
In addition to what tlhintoq said (you will need to use a base class) the reason your code doesn't work is because you're defining CurrentV in the switch block. When you exit, you go out of scope and lose your data :)
This is why you need the base type, so you can assign your generic data to it in your switch statement. Additionally, you can still tell what type it is...
example: - MyBaseType data = null;
-
switch (ClassID)
-
{
-
case 0: data = new Data0(); break;
-
case 1: data = new Data1(); break;
-
...
-
case n: data = new DataN(); break;
-
}
-
-
...
-
// We can tell what data is with the "is" statement
-
if (data != null)
-
{
-
if (data is Type0) Console.WriteLine("We have something of Type0!");
-
-
// or more specifically...
-
Console.WriteLine(data.GetType().ToString());
-
}
Here's the MSDN on inheritance. There's plenty more to be found via Google if this doesn't explain it well enough for you. http://msdn.microsoft.com/en-us/library/ms173149.aspx
Good luck!
| | Member | | Join Date: Sep 2009 Location: Usa, Michigan, Westland
Posts: 49
| | | re: Dynamically selecting Class for use in program.
I never really did get the Inheritance concepts really.
Also I don't have any methods within these classes. Its all just data variables.
Ugh. Im lost. =\
| | Member | | Join Date: Sep 2009 Location: Usa, Michigan, Westland
Posts: 49
| | | re: Dynamically selecting Class for use in program.
Unfortunally. Regardless of inheritance, Istill have different data sets, different class calls. Inheritance is only going to give me a generalized data set standard... It does nothing for me.
| | Familiar Sight | | Join Date: Jul 2009 Location: Calgary, Alberta, Canada
Posts: 233
| | | re: Dynamically selecting Class for use in program.
Yes but you're using a common type to hold the specific types so you can only deal with one variable.
Here's a quick and dirty example, I'm going to use public members to cut down on typing, you probably shouldn't code like this ;)
Declare some types... - class BaseType
-
{
-
}
-
-
class DataType1 : BaseType
-
{
-
public int Age;
-
}
-
-
class DataType2 : BaseType
-
{
-
public string Name;
-
}
Now decide what's kind of data we're going to set up. -
BaseType myItem = null;
-
int flag = 0; // or whatever you want
-
switch (flag)
-
{
-
case 0:
-
{
-
DataType1 newItem = new DataType1();
-
newItem.Age = 27;
-
myItem = newItem;
-
break;
-
}
-
case 1:
-
{
-
DataType2 newItem = new DataType2();
-
newItem.Name = "GaryTexmo";
-
myItem = newItem;
-
break;
-
}
-
}
myItem is going to contain the data you want, but it won't be accessible because myItem is of type BaseType, which has absolutely nothing in it. If I want to access the data, I can do the following... - if (myItem != null)
-
{
-
if (myItem is DataType1)
-
{
-
Console.WriteLine("Item is DataType1...");
-
Console.WriteLine(" Age is " + ((DataType1)myItem).Age);
-
}
-
else if (myItem is DataType2)
-
{
-
Console.WriteLine("Item is DataType2...");
-
Console.WriteLine(" Name is " + ((DataType2)myItem).Name);
-
}
-
else
-
{
-
Console.WriteLine("Unrecognized type...");
-
}
-
}
So while inheritance gives you the generalized type to pass around your program, you can always get back to the more specific type. This is what you said you wanted to do in your first post.
Your alternative would be to create a whole bunch of member variables for each type and assign them to null, only ever accessing the one for the type you want. This will work, but it's a lot of overhead and wasted code.
Does that help any?
| | Member | | Join Date: Sep 2009 Location: Usa, Michigan, Westland
Posts: 49
| | | re: Dynamically selecting Class for use in program.
So basicly what your trying to hint at is this...
Declare a class with all the properties setup, then toss the data into those properties at run time when user makes selection...
This is the fundamental reason why I'm tryin to figure this out in the first place...
Data store and retrieval. If I really can't use a predefined class with data in it... Now what? *sigh*
| | Familiar Sight | | Join Date: Jul 2009 Location: Calgary, Alberta, Canada
Posts: 233
| | | re: Dynamically selecting Class for use in program.
No, that's what I'm saying you should NOT do :) I'm saying you should use a base type to store whatever you need instead of instantiating an object for each specific type. Hence the code I posted.
I keep going over what you wrote in your original post and it sounds like this is pretty much what you need. Am I understanding you incorrectly? Perhaps you can further define what you're trying to do.
I apologize if I'm misunderstanding you.
| | Newbie | | Join Date: Oct 2009
Posts: 2
| | | re: Dynamically selecting Class for use in program.
You can use Reflection to instantiate the class if you can have the full path with namespace as a string.
string Namespace = "ABC.MyCode";
string fullPath = NameSpace + "." + "Data" + ClassID ;
Assembly ass = Assembly.GetExecutingAssembly();
object o = ass.CreateInstance(fullPath);
To get a property you can use reflection again.
NOTE: Reflection is not very efficient but its good sometimes.
| | Member | | Join Date: Sep 2009 Location: Usa, Michigan, Westland
Posts: 49
| | | re: Dynamically selecting Class for use in program.
Gary, This is basically what I have on my program right now. - public class TClass1 {
-
int[] TPoints1;
-
int[] TPoints2;
-
int[] TPoints3;
-
-
string[] TTitles1;
-
string[] TTitles2;
-
string[] TTitles3;
-
-
string[] TIconImg1;
-
string[] TIconImg2;
-
string[] TIconImg3;
-
-
string[][] TDiscription1;
-
string[][] TDiscription2;
-
string[][] TDiscription3;
-
}
That is the exact setup. Lacking the actual data.
And mind you I have only the first 2 classes 100% finished and filled with data, and the file is over 100k in text. So forgive me if I don't post it. =P
Besides Gary, I'm the one misunderstanding. Lol. I don't get the above posted code... Sorry!
>>PMohanta
Neat piece of code! That reminds me of PHP so much. lol. I resume my coding in the morning. I'll see what that thing does. =D
| | Familiar Sight | | Join Date: Jul 2009 Location: Calgary, Alberta, Canada
Posts: 233
| | | re: Dynamically selecting Class for use in program.
What are you looking to do with that code, though? I thought I understood your fundamental question from your top post, but now I'm not so sure. Quote:
Originally Posted by Samishii23 Besides Gary, I'm the one misunderstanding. Lol. I don't get the above posted code... Sorry! If that's the case then play around with it. You should be able to put that code into your compiler and test it. This is no different than how every class in C# inherits from object. So you can assign an integer to an object, or a string to an object... as such: - object myObj = 5;
-
object myObj2 = "this is a test";
Try reading through a few google articles on inheritance... even if it turns out to not be what you're looking for, it will certainly come in handy at some point :)
| | Member | | Join Date: Sep 2009 Location: Usa, Michigan, Westland
Posts: 49
| | | re: Dynamically selecting Class for use in program.
This is the do something function when the user makes the selection.
This is the very first version of it, so it'll be messy. Its the first step, and not the last. I have much much more to add to it still. Not even joking. lol.
Remember is the very first version of all the practice projects in the past in one method. Its sloppy, and in-efficient. But I hope to learn from it and maybe find a better way of doing it.
|  | Similar C# / C Sharp bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|