A better way than multiple case in switch statement
-
Hello. I have the following function:
public BaseClass SpawnClass( ObjectType type ) { BaseClass retval = null; float X = 5; float scale = 0.3429f; switch(type) { case ObjectType.Class1: { retval = new Class1(X,scale); break; } case ObjectType.Class2: { retval = new Class2(X,scale); break; } case ObjectType.Class3: { retval = new Class3(X,scale); break; } } return retval; }
I have about 11 classes but I didn't want this to get too long. This works, but it's very ugly. Is there a better way to do this? I've thought about the "Gang of Four" design patterns, but I'm not sure if they would be less ugly. I noticed the TypeConverter class, but I don't know how to use it. Any suggestions?I love to program!
-
Hello. I have the following function:
public BaseClass SpawnClass( ObjectType type ) { BaseClass retval = null; float X = 5; float scale = 0.3429f; switch(type) { case ObjectType.Class1: { retval = new Class1(X,scale); break; } case ObjectType.Class2: { retval = new Class2(X,scale); break; } case ObjectType.Class3: { retval = new Class3(X,scale); break; } } return retval; }
I have about 11 classes but I didn't want this to get too long. This works, but it's very ugly. Is there a better way to do this? I've thought about the "Gang of Four" design patterns, but I'm not sure if they would be less ugly. I noticed the TypeConverter class, but I don't know how to use it. Any suggestions?I love to program!
Assuming the constructors are identical, I'd probably do something like:
public BaseClass SpawnClass(System.Type type) { float X = 5; float scale = 0.3429f; return (BaseClass) type.InvokeMember(type.FullName, System.Reflection.BindingFlags.CreateInstance, null, null, new object[] {X, scale}); }
That should work from what I remember, but it may need some tweaks to be work correctly.----- In the land of the blind, the one eyed man is king.
-
Assuming the constructors are identical, I'd probably do something like:
public BaseClass SpawnClass(System.Type type) { float X = 5; float scale = 0.3429f; return (BaseClass) type.InvokeMember(type.FullName, System.Reflection.BindingFlags.CreateInstance, null, null, new object[] {X, scale}); }
That should work from what I remember, but it may need some tweaks to be work correctly.----- In the land of the blind, the one eyed man is king.
-
Assuming the constructors are identical, I'd probably do something like:
public BaseClass SpawnClass(System.Type type) { float X = 5; float scale = 0.3429f; return (BaseClass) type.InvokeMember(type.FullName, System.Reflection.BindingFlags.CreateInstance, null, null, new object[] {X, scale}); }
That should work from what I remember, but it may need some tweaks to be work correctly.----- In the land of the blind, the one eyed man is king.
-
That function looks like it depends on System.type, however the function I have depends on a custom enumeration (ObjectType). Would this way still work? Would there be a way to create a type using a string?
I love to program!
allenmpcx wrote:
Would there be a way to create a type using a string?
Activator.CreateInstance
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook