gout
gout

Reputation: 812

How do I create an enum-like object with string values?

i'm working on parsing a text project where in , i have to match the match the text and get the keywords from the text and perform some actions accordingly.

Now , i'm trying to use enum for matching the text Eg. all the conditions, any condition, none, alteast one,and or etc. i'm trying use enum as the key words might change later, Is it possible to store string values in enum.

public enum condition 
{ 
    type1 = "all the conditions", 
    type2 = "any of the conditions" 
}

i know it is not like normal enum usage,can any one help

Upvotes: 1

Views: 1439

Answers (3)

gideon
gideon

Reputation: 19465

You could use readonly string properties:

public class Condition
{
    public static readonly string Type1 = "All_The_Conditions";
    public static readonly string Type2 = "Any_Conditions";
}

Use it like this:

if(condition_variable == Condition.Type1)//will do a string compare here.
{
 ...
}

BUT

This above solution would however NOT work with switch statements. In which case you could use const

public class Condition
{//this could be a better solution..
    public const string Type1 = "All_The_Conditions";
    public const string Type2 = "Any_Conditions";
}

You could use it like this:

switch (condition_variable)
{
    case Condition.Type1://can only be done with const
     ....
    break;
}

See this post for static readonly vs const variables.


To Expand on enums (See MSDN):

They have a default underlying type of int. You can change the underlying type to one of the following integral types : byte, sbyte, short, ushort, int, uint, long, or ulong.


Thanks to @Bryan and @R0MANARMY for helping me improve my answer.

Upvotes: 4

Yuval Peled
Yuval Peled

Reputation: 5038

You can use a dictionary instead to map from the enum (key) to string (value). Something like:

Dictionary<Condition, string> dict = new Dictionary<Condition, string>(); 
dict[Condition.Type1] = "all the conditions";

[Edit]: Actually, now that I read your question more carefully I would do it the other way around. The mapping should be from the string to the condition and then you should compare the text to the key value (string) and if it matches you get the enum value. i.e:

   Dictionary<string, Condition> dict = new Dictionary<string, Condition>(); 
   Condition result = Condition.Invalid;

   bool isFound = dict.TryGetValue(someTextToParse, out result);

Makes sense?

Upvotes: 3

M.Babcock
M.Babcock

Reputation: 18965

I was under the impression that enum definitions had to contain numerical values, though I could be wrong.

An alternative way to handle this is with a simple array of struct objects:

struct ConditionKeywords
{
    int Key;
    string Value;
}
ConditionKeywords[] keyword = { new ConditionKeywords { Key = 1, Value = "all the conditions } /* ... */ };

And a simple enumeration which can be accessed in code:

enum ConditionValues
{
    type1 = 1;
}

Of course this has the potential to have multiple strings which mean the same key which is a double edge sword, so a simpler approach could be:

string[] ConditionKeywords { "all the conditions" /* ... */ }

using the same enumeration approach above (only limiting it to only valid indexes in ConditionKeywords).

Upvotes: 0

Related Questions