1

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

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
gout
  • 802
  • 10
  • 32

3 Answers3

4

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.

Community
  • 1
  • 1
gideon
  • 19,329
  • 11
  • 72
  • 113
  • +1 Enumerations are numeric types by definition, so they don't support strings. It might be worthwhile to mention that you can't use this solution with `switch` statements. – Bryan Watts Mar 01 '12 at 04:35
  • @BryanWatts excellent catch. R0MANARMY yep was thinking on the same lines and updated the question :) – gideon Mar 01 '12 at 04:44
  • @R0MANARMY ah typo, I copy pasted the prev code block, Fixed it. (I used something like this in a project I have opened at the moment ;) – gideon Mar 01 '12 at 04:49
3

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?

Yuval Peled
  • 4,988
  • 8
  • 30
  • 36
  • You'd have to test to see if the key is in the dictionary to avoid runtime exceptions. It might just serve to obscure the intent of the code. – Roman Mar 01 '12 at 04:37
  • Yes, and given that OP needs to parse text, why not have the string (or a hash thereof) as the key. The enum key can then be mapped e.g. with TryGetValue http://msdn.microsoft.com/en-us/library/bb347013.aspx – StuartLC Mar 01 '12 at 04:42
  • This seems awfully heavy compared to the behavior the OP is asking for. – M.Babcock Mar 01 '12 at 04:52
  • I would argue it obscures the intent a little bit. A lookup table implies the text options aren't fixed, seeing a bunch of `if` or a `switch` statement implies the number of text options if fixed. Either option would work. – Roman Mar 01 '12 at 04:55
  • It depends also on the number of words that he has to match against. Dictionary consumes more space but it's O(1) retrieval instead O(N). – Yuval Peled Mar 01 '12 at 04:58
0

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).

M.Babcock
  • 18,753
  • 6
  • 54
  • 84