Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Going more agnostic
Message
 
To
04/09/2009 13:11:22
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01422695
Message ID:
01422802
Views:
75
Hi Jim,

I would use a table to avoid a large switch statement for creating objects. Assuming you have an enum ElementType which defines the various classes and an interface IElement which is common to all objects that are created, you could use the following table. The order of the elements needs to match the enum:
static Func<IElement>[] Make = {
	()=>{return new BitElement();},
	()=>{return new StringElement();},
	()=>{return new NumberElement();}
};
to create an instance you would use the following code:
ElementType type = ElementType.String;
IElement element = Make[(Int32)type]();
This would avoid reflection.
--
Christof
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform