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

Re: system.enum.getvalues by Jon

Jon
Mon Oct 23 16:15:52 CDT 2006

Jeff Mason <je.mason@comcast.net> wrote:
> 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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Re: system.enum.getvalues by Ben

Ben
Mon Oct 23 16:32:02 CDT 2006


"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1fa74162e291569b98d579@msnews.microsoft.com...
> Jeff Mason <je.mason@comcast.net> wrote:
>> 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.

>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too



Re: system.enum.getvalues by Jon

Jon
Mon Oct 23 16:39:03 CDT 2006

Ben Voigt <rbv@nospam.nospam> wrote:
> > 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.


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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Re: system.enum.getvalues by Ben

Ben
Tue Oct 24 08:17:55 CDT 2006


"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1fa746ce8832a1dd98d57b@msnews.microsoft.com...
> Ben Voigt <rbv@nospam.nospam> wrote:
>> > 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.
>
> 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.
>
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=156103

The MSDN page has a rating of "1", the worst possible, as well.

Microsoft just wants to sell their "Certified Developer" courses and bad
public documentation helps them do that.

> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too



Re: system.enum.getvalues by Lee

Lee
Tue Oct 24 13:37:11 CDT 2006


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.

--
// Lee Silver
// Information Concepts Inc.


> 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