Re: passing enum value as an argument
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Wed, 13 Jul 2005 22:23:53 +0100
Madestro <me_no_like_spam_juanDOTromero@bowneDOTcom> wrote:
> > Yes, but then you made blanket statements which didn't state that you
> > were essentially working in a non-typesafe way. You can't expect type
> > safety at all (at compile time) when you've got option strict turned
> > off. Do you actually write much code without option strict on?
>
> The issue at hand in this particular question is whether you could possibly
> call a value from an Enum without qualifying the value, the discussion is not
> about type safety.
It is when you say that getting rid of enforcing qualification removes
the whole point of enums - because that dismisses type safety as being
part of the useful purpose of using enums.
> Anytime you assign a type of any kind to any variable you
> get type safety, there is no argument there. It was the other reply that
> introduced type safety into this thread. By the way, I ALWAYS code with
> Option Strict ON.
Then why bother claiming that something succeeds (without saying under
what circumstances) when it would *never* succeed in the way you code?
> > Yes, if you cast it. In what way does that diminish the usefulness of
> > having compile-time type safety when you're *not* deliberately trying
> > to break things?
>
> Again. Not talking about type safety here.
But I am, because it's a fundamental part of the purpose of enums.
> > By your idea of the purpose of enums, yes, the OP's solution would
> > indeed defeat the purpose. I think you'll find you're in the minority
> > when it comes to that idea.
>
> This may sound silly but.... For you to get the benefits of an Enum, you
> have to USE the Enum, don't you think?... lol, no disrespect there. Consider
> this:
>
> Enum Juan
> One
> Two
> End Enum
>
> Dim SomeVariable as Juan
> When you call it: SomeVariable = Juan.One
>
> There you are limiting the value of SomeVariable to a limited range (the
> Enum). Now, This is what the owner of this post proposes:
>
> Enum Juan
> One = 0
> Two = 1
> End Enum
> Dim SomeVariable as Juan
> When you call it: SomeVariable = One
>
> First of all, how will the compiler be able to resolve this variable?
By noting that the type is SomeVariable, that there is no other member
One which is available, and that One is a member of the Juan enum. It
could reasonably infer that Juan.One is what you're after.
> It will not be able to, because in this scope there is no such variable.
Who says it has to resolve to a variable?
> There is a reference however to Juan.One . If the user wants to not
> qualify the name (which you can never do), wouldnt it be the same as
> doing this?:
>
> Dim SomeVariable as Juan
> Dim One as integer = 0
> When you call it: SomeVariable = One
No, it wouldn't - because the compiler would be able to notice that you
were referring to Juan.One implicitly, and that that was a reasonable
value for SomeVariable (because it's an element of Juan), whereas an
integer itself isn't an element of Juan. (Admittedly the constant 0 is
implicitly convertible to any enum, but that's a side issue.)
> The only difference would be that the enum would limit you to a defined
> range.
Again, it doesn't, as I've shown *several* times now. Please, please
compile one of the complete programs I've provided which shows it *not*
limiting anything to a defined range. It's just a case of copy, paste
into new text file, compile, and run.
> Other than that, this is the same, so IT DOES defeat the purpose of
> having an Enum, which is why Enums exist, otherwise the language creators
> wouldnt have bothered, don't you think?
No, it doesn't defeat the purpose because type safety isn't lost.
You *could* declare three variables:
Dim One as Juan = Juan.One
etc
but doing that everywhere you use the enum would get old very quickly -
and it's unnecessary.
For what it's worth, Java now has the ability to do "static imports"
which allow for precisely this kind of thing - and guess what? The
world keeps moving, and you don't lose any of the benefits of enums
etc. (Admittedly Java enums are somewhat different to .NET enums -
vastly superior IMO - but the principle is the same.)
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.
- References:
- passing enum value as an argument
- From: Glenn Venzke
- RE: passing enum value as an argument
- From: Jon Skeet [C# MVP]
- RE: passing enum value as an argument
- From: Madestro
- RE: passing enum value as an argument
- From: Jon Skeet [C# MVP]
- RE: passing enum value as an argument
- From: Madestro
- RE: passing enum value as an argument
- From: Jon Skeet [C# MVP]
- RE: passing enum value as an argument
- From: Madestro
- Re: passing enum value as an argument
- From: Jay B. Harlow [MVP - Outlook]
- Re: passing enum value as an argument
- From: Madestro
- Re: passing enum value as an argument
- From: Jon Skeet [C# MVP]
- Re: passing enum value as an argument
- From: Madestro
- passing enum value as an argument
- Prev by Date: RE: passing enum value as an argument
- Next by Date: Re: Public varaible QueryString issue
- Previous by thread: Re: passing enum value as an argument
- Next by thread: RE: passing enum value as an argument
- Index(es):
Relevant Pages
|