Re: enumerations as parameters, VB, and the value 0
From: Plausible Indirection (cg22165_at_yahoo.com)
Date: 08/17/04
- Next message: Plausible Indirection: "Re: enumerations as parameters, VB, and the value 0"
- Previous message: Rick Byers [MSFT]: "RE: type initializers"
- In reply to: Jay B. Harlow [MVP - Outlook]: "Re: enumerations as parameters, VB, and the value 0"
- Next in thread: Jay B. Harlow [MVP - Outlook]: "Re: enumerations as parameters, VB, and the value 0"
- Reply: Jay B. Harlow [MVP - Outlook]: "Re: enumerations as parameters, VB, and the value 0"
- Messages sorted by: [ date ] [ thread ]
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...
- Next message: Plausible Indirection: "Re: enumerations as parameters, VB, and the value 0"
- Previous message: Rick Byers [MSFT]: "RE: type initializers"
- In reply to: Jay B. Harlow [MVP - Outlook]: "Re: enumerations as parameters, VB, and the value 0"
- Next in thread: Jay B. Harlow [MVP - Outlook]: "Re: enumerations as parameters, VB, and the value 0"
- Reply: Jay B. Harlow [MVP - Outlook]: "Re: enumerations as parameters, VB, and the value 0"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|