Avi
Avi

Reputation: 16176

Failing to assign a value to an enum

Did you ever have a day when nothing works? Now I can't even succeed in setting the value of an enum!

I was having trouble with the Enum.Parse statement at the bottom, so I've written the 'if' block above it. To my surprise, this also failed.

I traced this with the debugger. The value of the string x is “OnDemand”. The first 'if''s "true" branch is taken, but bitmapCacheOption stays BitmapCacheOption.Default.

Ditto for the Enum.Parse expression below. So my question is: What am I doing wrong in assigning value to an enum?

BitmapCacheOption bitmapCacheOption;
if (x == "OnDemand") bitmapCacheOption = BitmapCacheOption.OnDemand;
else
{
   if (x == "OnLoad") bitmapCacheOption = BitmapCacheOption.OnLoad;
   else
   {
      if (x == "None") bitmapCacheOption = BitmapCacheOption.None;
      else bitmapCacheOption = BitmapCacheOption.Default;
   }
}
BitmapCacheOption bitmapCacheOption1 =
            (BitmapCacheOption)Enum.Parse(typeof(BitmapCacheOption), x, false);
Debug.Assert(bitmapCacheOption == bitmapCacheOption1);

Edit: The enum in quesion is a WPF one, BitmapCacheOption. The false at the end of the Enum.Parse statement just means to ignore the case. I'm aware of better ways to write a cascading 'if' statement (including "else if" and "switch" statements), but all this is beside the issue. I've written the 'if' this way during debugging to allow me to step through with the debugger. What is important is that even with the simple if, when x equals "OnDemand" bitmapCacheOption stays BitmapCacheOption.Default!

edit: Notice the value of "bitmapCacheOption" in the Debugger's Locals window - it stays in "Default" even though the yellow highlight displays that the "OnDemand" swithc case wast taken!

Debugger Snap:

Upvotes: 0

Views: 367

Answers (2)

ken2k
ken2k

Reputation: 48985

You're doing nothing wrong here...

Look at the definition of the Enum:

// Summary:
//     Specifies how a bitmap image takes advantage of memory caching.
public enum BitmapCacheOption
{
    // Summary:
    //     Creates a memory store for requested data only. The first request loads the
    //     image directly; subsequent requests are filled from the cache.
    OnDemand = 0,
    //
    // Summary:
    //     Caches the entire image into memory. This is the default value.
    Default = 0,
    //
    // Summary:
    //     Caches the entire image into memory at load time. All requests for image
    //     data are filled from the memory store.
    OnLoad = 1,
    //
    // Summary:
    //     Do not create a memory store. All requests for the image are filled directly
    //     by the image file.
    None = 2,
}

OnDemand and Default have both 0 as value ;)

So you can't rely on the string representation of 0 casted as a value of BitmapCacheOption.

Upvotes: 2

Royi Namir
Royi Namir

Reputation: 148624

try removing the false operator -

BitmapCacheOption bitmapCacheOption1 =
            (BitmapCacheOption)Enum.Parse(typeof(BitmapCacheOption), x);

And on the way - Upload the Enum structure for us to see if there is a problem.

edit:wow !

enter image description here

it looks like they have a bug - both index with 0. This is why you get the default every time. because it is 0. its fine ... but when you set the value to x - it assigns him the 0 instead anothe YYY value which should be there....

Upvotes: 4

Related Questions