tags:

views:

326

answers:

6
+3  Q: 

Flags enum in .NET

I am trying to use a set of conditional statements that will set up an enumeration attributed with [Flags]. However, the compiler complains that 'm' is unassigned. How can I rewrite the following to achieve my intended functionality?

Media m;
if (filterOptions.ShowAudioFiles)
    m = m | Media.Audio;
if (filterOptions.ShowDocumentFiles)
    m = m | Media.Document;
if (filterOptions.ShowImageFiles)
    m = m | Media.Image;
if (filterOptions.ShowVideoFiles)
    m = m | Media.Video;
+1  A: 

If none of the conditions are true m will be undefined. Set it to an initial value.

EricSchaefer
+16  A: 

You need to initialize m. Create a "None" flag that has value 0 then:

Media m = Media.None;

Then the rest of your code.

jeffamaphone
+1  A: 

Do you have a 'default' like filterOptions.ShowNone? If so, start off with m set to that. The compiler is complaining becuase at the end of all the if's, m might not be set to anything.

n8wrl
A: 
Matthew Whited
What if all the conditions fail? What if 0 is e.g. Media.Audio? You need a *valid* initial value.
EricSchaefer
This is bad advice. You are always better off using explicit values than relying on integer casts.
Randolpho
Not really. The enum will still work perfectly fine even if there isn't a "valid" flag set. There will still be a value. All enums are inherited from "Int32" by default. Yes there could be other problems later if you don't handle default values. But that is what exceptions are for.
Matthew Whited
Since it's a flags enum, the None value ought to map to 0 (no bits set). Doing it any other way would just be weird. On the other hand you may want a Media.Default which does have some bits set.
tvanfosson
Ouch. So you wait until later when an exception is raised rather than initializing your data properly in the forst place? Ouch again.
EricSchaefer
Matthew Whited
@Matthew: If someone were to use Media.None in that fashion, it's their own damn fault for not understanding enums.
Samuel
A: 

In addition to the above answers, besides the fact that this code seems pretty redundant, I'd like to suggest that you use a Select Case instead of all those ugly If's.

hmcclungiii
Considering that any combination of the flags could be set, I don't see how a Select Case would be appropriate.
Daniel LeCheminant
Agreed, -1 because you could not replace those ifs with a switch.
Samuel
Ok, so I had a good healthy brainfart there.
hmcclungiii
+1  A: 

You could also write:

Media m = default(Media)

Useful in cases where you don't know the enum, class, or whether it's a value/reference type.

David Cuccia
This is really a bad idea, it hides the whole simplicity of enumerations. They were created to remove magic numbers and you are adding it back in when there should be a None for a flags enum that you should use instead.
Samuel
Good point. I was thinking about the case where you aren't the author, but then explicit choice of the default would be better anyway.
David Cuccia