Hello,
I’ve got a web service that has values in enums like so, its generated automatically:
and I have a method that takes in string values to select the correct enum value like so:
and its used like so:
My issue is, if lets say I use the above value with 01 it returns Item02, it always seems to return a +1 in the value, does anyone have any idea why? I’m so lost!
More examples below
Thanks for the help!
I’m not sure I quite get what your problem is.
The enum at the top of your post is equivalent to:
That’s how enums work. Like any other value type, the default is to start with 0.
If you use any kind of Enum.Parse
with a text string that equates to value n
, you’re going to get the nth value in the list. When n == 1, you’ll get “Item02” because that’s the item.
If instead you use the complete name, such as
Then (in this case) you’ll get the 0th item and (int)value
would equal 0.
So what do you WANT to happen that you don’t think is working correctly? Are you expecting that Enum.Parse
is going to respect the System.Xml.Serialization.XmlEnumAttribute
? It’s not.
Edit/Side note: If your enum is non-contiguous like so:
and you use
Then you’ll get an odd/strange result. In this case, value
will be the datatype test
, but (int)value
will equal 1 and won’t be in the actual enum list and you won’t be able to compare it easily to members of the enum.
Oh cool, interesting. Yah that’s what I was expecting, thanks for clarifying. The way it works is, example in question is a dropdown and I figured it would just be easier to set dropdown values to 01, 02 etc. and then send that value to API and get the enum value based on that, guess that wouldnt work then. hmmm
You can set Item01 = 1;
Currently it is set to the int default value of 0
thanks for the reply! its a generated class from a wsdl file and theres probably 50 other enums, I don’t really want to go through each one and do that
C# devs
null reference exceptions