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

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+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.
@BryanWatts excellent catch. R0MANARMY yep was thinking on the same lines and updated the question :)
@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 ;)
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?

5 Comments

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.
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 msdn.microsoft.com/en-us/library/bb347013.aspx
This seems awfully heavy compared to the behavior the OP is asking for.
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.
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).
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).

2 Comments

Wow, I missed a lot while typing my answer.
I know the feeling! (From most other questions)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.