Home All Groups Group Topic Archive Search About

system.enum.getvalues

Author
23 Oct 2006 8:58 PM
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

Author
23 Oct 2006 9:15 PM
Jon Skeet [C# MVP]
Jeff Mason <je.ma***@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 - <sk***@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
Author
23 Oct 2006 9:32 PM
Ben Voigt
Show quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.1fa74162e291569b98d579@msnews.microsoft.com...
> Jeff Mason <je.ma***@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.

Show quote
>
> --
> Jon Skeet - <sk***@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
Author
23 Oct 2006 9:39 PM
Jon Skeet [C# MVP]
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 - <sk***@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
Author
24 Oct 2006 1:17 PM
Ben Voigt
"Jon Skeet [C# MVP]" <sk***@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.

Show quote
> --
> Jon Skeet - <sk***@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
Author
24 Oct 2006 6:37 PM
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.

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


Show quote
> 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

AddThis Social Bookmark Button