Re: enumerations as parameters, VB, and the value 0

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Jay B. Harlow [MVP - Outlook] (Jay_Harlow_MVP_at_msn.com)
Date: 08/18/04


Date: Wed, 18 Aug 2004 08:45:26 -0500

Chris,
> I guess you could argue that the language definition itself _makes_ 0
> a more valid value than 2, but that is just a hair-puller for me.
Unfortunately with VS.NET 2002 & VS.NET 2003 you will be losing some hair.
:-|

As that is how it is currently defined to work! I stated I have not tried
VS.NET 2005 (aka Whidbey, due out in 2005) to see if things are improved or
not. I really hope they will be!

> On top of that, I tried
>
> dt = New Foo(CInt(0), Bar.Bar0)
Try

    Dim zero As Integer = 0

    dt = New Foo(zero, Bar.Bar0)

As that is the only way I know of to get VS.NET 2002 & VS.NET 2003 to work.
Yes MS knows there is a problem! As I told them a year ago when I came
across this quirk and remembered them when you asked your original question.

> Enum Bar
> Bar0 = -1
> Bar1 = -2
> Bar2 = -4
> End Enum

Remember that

    Dim myBar As Bar

Will have the value of literal 0 per the CTS. Also remember that the CLR
does not validate values within an Enum, if you need validation of the
values you can use Enum.IsDefined.

    If Not [Enum].IsDefined(GetType(Bar), myBar) Then
        Throw New ArgumentOutOfRangeException("myBar", myBar, "Invalid Enum
Bar value!")
    End If

Hope this helps
Jay

"Plausible Indirection" <cg22165@yahoo.com> wrote in message
news:7b9ebe1f.0408170912.31a101a6@posting.google.com...
> I've been busy for a few days and I came back to a lot more discussion
> on this than I expected at first. Thanks to all!
>
> Ok, there seems to be some sloppiness in the language spec.
> Basically, it does not enforce the restriction of binding of a value
> to an enumeration type to be one of the values defined for that type.
> Because of this lack of enforcement, it creates an implicit cast from
> 0 to enum that would otherwise not always be possible. It is quite
> possible to define an enumeration that does not contain a valid value
> for 0.
>
> On top of that, I tried
>
> dt = New Foo(CInt(0), Bar.Bar0)
>
> I would think that the conversion function would remove any
> possibility that the 0 could be interpreted as an enum, but the
> behavior remains the same. This just doesn't make sense to me.
>
> I also tried
>
> Enum Bar
> Bar0 = -1
> Bar1 = -2
> Bar2 = -4
> End Enum
>
> and again there was no behavior change. This really seems like a
> weakness in the language because, it would be a whole lot simpler, and
> IMHO, better, to define one rule for interpreting numeric literals as
> a type rather than multiple rules based on what the value happens to
> be. Ok, I'd allow promotion along the integer types, but still, in
> this case, the literal 0 is no more a valid value for the enumeration
> than the literal 2.
>
> I guess you could argue that the language definition itself _makes_ 0
> a more valid value than 2, but that is just a hair-puller for me.
>
> -Chris
>
>
> "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message
news:<OwpU#W5gEHA.2916@TK2MSFTNGP12.phx.gbl>...
> > Code,
> > A widening conversion is one where there is no loss of data. Integer
allows
> > all values from Int32.MinValue to In32.MaxValue, while Enum only allows
(not
> > enforced) the values that are defined on it (a sub set of Integer).
> >
> > A narrowing conversion is one where there may be a loss of data.
> >
> > A widening conversion is an implicit cast operation, while a narrowing
> > conversion is an explicit cast operation.
> >
> > As Jon pointed out literal 0 is special in that an implicit cast is
allowed,
> > while the literal 2 an explicit cast is required. Mostly because we know
> > that 0 is valid for the Enum (as it is its "default" value) while 2 may
or
> > may not have been defined in the Enum.
> >
> > Hope this helps
> > Jay
> >
> > "cody" <no_spam_deutronium@gmx.net> wrote in message
> > news:%23ef43v2gEHA.2916@TK2MSFTNGP12.phx.gbl...



Relevant Pages

  • Re: enumerations as parameters, VB, and the value 0
    ... to an enumeration type to be one of the values defined for that type. ... Because of this lack of enforcement, it creates an implicit cast from ... to enum that would otherwise not always be possible. ... > A widening conversion is one where there is no loss of data. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Designing a Card Game
    ... an enum declaration specifies a new named reference type. ... widening reference conversion, which is a permitted form of method ... private Card(Rank rank, Suit suit) { ... I wonder if there was a compiler bug at some point or if I just have an advanced case of CRS. ...
    (comp.lang.java.programmer)
  • Re: Designing a Card Game
    ... an enum declaration specifies a new named reference type. ... widening reference conversion, which is a permitted form of method ... I wonder if there was a compiler bug at some point or if I just have an advanced case of CRS. ... You know what I'll bet I know what my original problem was, the JDialog constructor takes either a Frame, a Dialog or a Window as owner. ...
    (comp.lang.java.programmer)
  • Re: enum + long
    ... compile-time constant expression. ... An enum requires an integral constant expression and the address of a ... if the conversion of the constant address into ... extern int foo, bar; ...
    (comp.lang.c)
  • Re: extending enum
    ... >> enum constants. ... >> superclass, plus add additional capabilities or constraints. ... casting is required when a conversion may fail ...
    (comp.lang.java.programmer)