Groups | Blog | Home
all groups > vb.net > february 2007 >

vb.net : Enum = Integer prevention


Zytan
2/13/2007 6:13:03 PM
Even with Option Strict On, which prevents an enum being set to an
Integer, you can still compare an enum to an integer without
complaint:

myEnum = 1 'fails
if (myEnum = 1) Then ... 'works

Is it possible to have strict type checking so strict that it prevents
all integer / enum interaction? I don't get why they would let this
one through.

Zytan
Michel Posseth [MCP]
2/14/2007 12:00:00 AM
Hello

A enum is intended to make a more readable parameter construct where you
would have otherwise used numeric values as parameters
[quoted text, click to view]

regards

Michel

"Zytan" <zytanlithium@yahoo.com> schreef in bericht
news:1171419183.342321.44240@k78g2000cwa.googlegroups.com...
[quoted text, click to view]

Zytan
2/14/2007 8:06:42 AM
[quoted text, click to view]

Yes, exactly. That's precisely what they are for. And this applies
to both cases:
1.
myStatus = 1 'bad code, illegal code
myStatus = InitiaitionStatus 'good code

2.

If myStatus = 1 Then 'bad code, LEGAL code
do_something()
Endif
If myStatus = InitiaitionStatus Then 'good code
do_something()
Endif

[quoted text, click to view]

Michel, i fail to follow this logic. You are simply explaining the
semantics of the language. so, yes, i understand that it doesn't
allow 'setting' but allows 'comparison'. But your words do not
explain any reason as to why the language is this way. In fact, your
first paragraph shows exactly why it shouldn't be this way.

But, thanks, because i think you have answered my original concern
that VB doesn't have complete strict type checking. (I assume there's
no way to force it to be so?)

This is sad, since, say, in the case of converting a variable that
stores a 'stage / status' as 0, 1, 2, etc. into an Enum, you cannot
rely on the language to find anything missed. I think it's a shame.
But, at least they did the half that was implemented, and its better
than nothing. i was just getting my hopes up that it was perfect.

Incidentally, for those that think i am being picky, i've already seen
MSDN tutorials on VB .NET that have mistakes since they are allowed to
use Integers for Enums, where if they had been forced to use the Enum,
the mistake would have been caught (and the example would have been
much easier to learn).

Zytan
AddThis Social Bookmark Button