Click here to Skip to main content
14,667,169 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to create string ENUM in c#.
Basically i wan't to set form name in Enum. When i open form in main page that time i want to switch case for form name and open that particular form.

How to set string in ENUM?
Any Idea about this?
Posted
Comments
   
Incorrect question. The set of words "Set string in enum" makes no sense.
--SA

Rate this:
Please Sign up or sign in to vote.

Solution 2

Please see my comment. This question cannot be answered, but I can give you some ideas.

First of all, if you define enumeration type, you can write a switch block using the enumeration values, not strings. Generally, 1) using strings for identification of any object is a very unreliable practice very bad for maintenance, 2) using Control.Name property in "regular" UI application is a very bad thing; this property is needed for the Designer, but your code should better use references to the objects, not name, which is not reliable (what if you misspell something? the compiler won't detect the problem).

If you want to create a form depending on switch, you should use the form type. If you somehow calculate the form type, you can create a form using this type through Reflection:
Form CreateForm(System.Type formType) {
    if (!typeof(Form).IsAssignableFrom(formType))
        throw new InvalidOperationException(
            string.Format("Form type expected, {0} passed",
                formType.FullName));
    System.Reflection.ConstructorInfo ci = //parameterless:
        formType.GetConstructor(System.Type.EmptyTypes);
    object form = ci.Invoke(null); //no parameters
    return (Form)form;
}

Now, I developed a technique to avoid long switch statement. The idea is to create a dictionary using some key type and some delegate type as the value type. You can use it to dispatch (actually call) a delegate instance by a key value. It's much better for maintenance than switch and better for performance if there are many keys. You can use enumeration type of System.Type as a key. Please see:
Dynamic Method Dispatcher[^].

For many interesting detail on working with enumeration types, please see my article Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^].

—SA
   
v5
Rate this:
Please Sign up or sign in to vote.

Solution 1

try like this
public enum AnalysisType { sss, ssss };
    protected void Page_Load(object sender, EventArgs e)
    {     
          
      string  name="sss";
      switch (name)
      {
        case AnalysisType.sss:
      }
    }
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100