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;
You need to initialize m. Create a "None" flag that has value 0 then:
Media m = Media.None;
Then the rest of your code.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With