0

I saw somewhere (cannot find it anymore) that you can check the existence of an enum value from an enum item with a specific list of items. Ex. below - "Available = doc | xls | csv"

But the following code does not seem to work. I'm expecting the results to be = xls instead of doc, since it is in the list of "Available" values.

Can someone please help?

Thanks in advance!

Niki

Button Code:

protected void btnTest01_Click(object sender, EventArgs e) { TestEnum01 result1 = TestEnum01.xls; TestEnum02 result2 = TestEnum02.xls; TestEnum03 result3 = TestEnum03.xls; if (result1 != TestEnum01.Available) { result1 = TestEnum01.doc; } if (result2 != TestEnum02.Available) { result2 = TestEnum02.doc; } if (result3 != TestEnum03.Available) { result3 = TestEnum03.doc; } this.txtTest01_Results.Text = String.Format("01: Result = {0}, Available = {1}\r\n\r\n02: Result = {2}, Available = {3}\r\n\r\n03: Result = {4}, Available = {5}", result1.ToString(), TestEnum01.Available, result2.ToString(), TestEnum02.Available, result3.ToString(), TestEnum03.Available); } 

ENUMS

public enum TestEnum01 { doc = 1, txt = 2, xls = 4, csv = 8, unknown = 5, Available = doc | xls | csv } public enum TestEnum02 { doc, txt, xls, csv, unknown, Available = doc | xls | csv } public enum TestEnum03 { doc, txt, xls, csv, unknown, Available = TestEnum03.doc | TestEnum03.xls | TestEnum03.csv } 

RESULTS:

01: Result = doc, Available = Available 02: Result = doc, Available = csv 03: Result = doc, Available = csv 
4
  • It is not really clear what is your question. Can you explain it better? Also those 3 enums are confusing. Are they shows your attempts or..? Commented Feb 16, 2015 at 19:33
  • My question is, can I check an enum item if it exists using another enum item with a list of values. In the example above, I'm checking ifof TestEnum01.xls exists in the list of values in TestEnum01.Available. But the "if (result1 != TestEnum01.Available)" does not seem to work. Commented Feb 16, 2015 at 19:38
  • 1
    Use HasFlag method if(result.HasFlag(TestEnum01.xls)) and to get nice ToString conversion, use FlagsAttribute Commented Feb 16, 2015 at 19:42
  • Thanks. I used the HasFlag to default the value to doc if enum is unknown or not in the list of Available values. "if (!TestEnum01.Available.HasFlag(input) || (input == TestEnum01.unknown))" Commented Feb 16, 2015 at 20:04

1 Answer 1

3

You have to use the FlagsAttribute :

[Flags] public enum TestEnum01 { doc = 1, txt = 2, xls = 4, csv = 8, unknown = 5, Available = doc | xls | csv } 

Then to test it :

TestEnum01 test = TestEnum01.doc | TestEnum01.txt; bool isDoc = (test & TestEnum01.doc) == TestEnum01.doc; 

Note that in your example, you will have a problem with unknown value, since binary wise, 1 | 4 = 5 ... And that means doc and xls produces unknown... To avoid this kind of problems, I prefer to use a direct bit-shift notation :

[Flags] public enum TestEnum01 { unknown = 0, doc = 1 << 0, txt = 1 << 1, xls = 1 << 2, csv = 1 << 3, Available = doc | xls | csv } 

If you just want to test for a particular flag, you can just use the HasFlag() method

TestEnum01 test = TestEnum01.doc | TestEnum01.txt; bool isDoc = test.HasFlag(TestEnum01.doc); 
Sign up to request clarification or add additional context in comments.

3 Comments

Sharped, how can I use that logic to test if the value I am passing (ex. TestEnum01.xls) exists in TestEnum01.Available?
just use the line bool isDoc = ... or HasFlag()
Thanks. I used the HasFlag to default the value to doc if enum is unknown or not in the list of Available values. "if (!TestEnum01.Available.HasFlag(input) || (input == TestEnum01.unknown))" where input=TestEnum01.xls for example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.