This is kind of an unusual question, and is probably more appropriate for .NET over TinyCLR…but I’ll ask anyways! (:
I’ve created a hashtable that contains a variety of different objects. These objects are not the built-in types like int, string, etc. but rather custom class objects like “LedController” for example.
I’ve successfully added the objects and pulled them out from the hash table again. Right now, I pull the object from the table, read the object’s Type, and use a switch-case comparing the Type.Name with hard-coded strings. An example is shown below:
//Read the object type
Type objectType = hashTableObject.Value.GetType();
//Determine object's type by using the Name property
switch (objectType.Name)
{
case "LedController":
//Do things with LED controller
break;
}
But I’d rather not use hard-coded strings if possible, as I’m sure the object names might change in the future (or decide to use inheritance later on). Does anyone have any suggestions? I’d also like to avoid using if/else blocks as there can be a good handful of types in the hashtable.
If anyone has any suggestions I’d appreciate the insight!
Thanks
How about adding a base class with a type enum and inherit base to all your classes. You could then cast hash table object to base and look at enum. You could change class names while keeping the type enum the same.
Something has to stay constant if want to change class names and not change identification code.
Do you specifically need to use a switch? If it’s trouble, you can use a Dictionary<string, int> that translates the name into an int that will work in the switch. However, I think C# for .net supports switching on string already.
var dictionary = new Dictionary<string, int>();
dictionary.Add("one", 1);
dictionary.Add("two", 2);
dictionary.Add("three", 3);
switch(dictionary["one"])
{
case 1:
Console.WriteLine("one");
break;
case 2:
Console.WriteLine("two");
break;
case 3:
Console.WriteLine("three");
break;
}
Switch statements are just syntactic sugar wrapped around ‘if’ statements. If you want to do this efficiently without adding enums or additional objects to your code, it’s easiest to let go of the switch construct and use typeof(). Using typeof in a cascading ‘if’ works with native types and doesn’t add to the memory footprint of your program:
var type = this.Value.GetType();
if (type == typeof(double)) {
...
} else if (type == typeof(string) {
...
} ...and so on
or if you are reflecting on the type of fields in a class or struct, instead of GetType() you can use .MemberType and .FieldType to walk through the fields and members and do the same sort of comparison to typeof(). Example: here
Thanks for the answers everybody! Sounds like there are a few good options each with their own pros and cons. The enum/dictionary route is good, allows me to continue using switch/case, but requires more setup and additional parameters when initializing. The typeof route is also good and has a simpler setup but have to use if/else instead. I’ll have to play around with it and see which way is better for my use case.