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

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Plausible Indirection (cg22165_at_yahoo.com)
Date: 08/17/04


Date: 17 Aug 2004 10:12:04 -0700

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
    ... > a more valid value than 2, but that is just a hair-puller for me. ... does not validate values within an Enum, if you need validation of the ... >> A widening conversion is one where there is no loss of data. ... >> A widening conversion is an implicit cast operation, ...
    (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)