system.enum.getvalues

J

Jeff Mason

I am observing some puzzling behavior with the GetValues method of enumerations. I
wonder if this something I just don't understand, or is this just wrong.

The documentation for the GetValues method says "The elements of the array [returned]
are sorted by the values of the enumeration constants". Further the docs state that
absent any underlying type definition of the enumeration, the type is assumed to be
int32 (a signed integer).


Consider the following code:


Public Enum MyEnum
Entry1
Entry2
Entry3
End Enum


Sub DisplayValues
For Each i As Integer In System.Enum.GetValues(GetType(MyEnum))
Debug.WriteLine(i.ToString)
Next
End Sub


As expected, the values displayed are 0, 1, and 2.


If I redefine the enumeration as:


Public Enum MyEnum
Entry1 = -1
Entry2 = 0
Entry3 = 1
End Enum


Then the values displayed are 0, 1, -1 (!)


It seems the values are sorted as *unsigned* integers.


Thus:


Public Enum MyEnum
Entry1 = -1
Entry2 = -2
Entry3 = 0
End Enum


displays 0, -2, -1 (!!)


What's going on here?


I've verified the same behavior occurs in both .NET 1.1 and 2.0.


-- Jeff



-- Jeff
 
J

Jon Skeet [C# MVP]

Jeff Mason said:
I am observing some puzzling behavior with the GetValues method of enumerations. I
wonder if this something I just don't understand, or is this just wrong.

The documentation for the GetValues method says "The elements of the array [returned]
are sorted by the values of the enumeration constants". Further the docs state that
absent any underlying type definition of the enumeration, the type is assumed to be
int32 (a signed integer).

I think it's just plain wrong, unfortunately. I suggest you use
http://connect.microsoft.com/VisualStudio to report it as a
documentation bug.
 
B

Ben Voigt

Jon Skeet said:
Jeff Mason said:
I am observing some puzzling behavior with the GetValues method of
enumerations. I
wonder if this something I just don't understand, or is this just wrong.

The documentation for the GetValues method says "The elements of the
array [returned]
are sorted by the values of the enumeration constants". Further the docs
state that
absent any underlying type definition of the enumeration, the type is
assumed to be
int32 (a signed integer).

I think it's just plain wrong, unfortunately. I suggest you use
http://connect.microsoft.com/VisualStudio to report it as a
documentation bug.

Sadly it seems that documentation bugs placed there get resolved as
"external" and nothing is done.
 
J

Jon Skeet [C# MVP]

Ben Voigt said:
Sadly it seems that documentation bugs placed there get resolved as
"external" and nothing is done.

Hmm... that's not been my experience, I have to say. The alternative is
to click on the link in the MSDN page at fault.
 
L

Lee

Jeff Mason wrote:

I reported this bug to Microsoft back in May or June. They acknowledged
the problem -- that the array is sorted as unsigned integers. They also
said that the behavior will not change as it may break existing code. I
asked them to update the documentation -- but I haven't heard back from
them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top