Re: passing enum value as an argument



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
.



Relevant Pages

  • Re: passing enum value as an argument
    ... | Like I said before, ultimately, the enum evaluates to a constant, so there ... I normally use Option Strict On to ensure type safety, ... | It may not be the case in the language you are using, ... | TakeSecondEnum--> FAILS ...
    (microsoft.public.dotnet.general)
  • Re: C# Enums - Feature or deficiency
    ... The C# enum is a "poor cousin", just because it won't break type safety for ... Lew, using an enum as the ordinal type for indexing an array doesn't ... compromise type safety, and it can even increase it. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# Enums - Feature or deficiency
    ... The C# enum is a "poor cousin", just because it won't break type safety for your convenience? ... It's not idiomatic C# to index arrays with enum types, ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: passing enum value as an argument
    ... TakeSecondEnum--> FAILS ... TakeSecondEnum--> SUCCEEDS ... Like I said before, ultimately, the enum evaluates to a constant, so there ... is really no "type safety" here, except for the type of the constant and the ...
    (microsoft.public.dotnet.general)
  • Re: VB6 compile error "constant expression required" with enum - solution
    ... On the enum line, put the cursor after 'enum', spacebar, arrow down. ... compile over, but you will be under way in 10 seconds or less. ... Posting for future reference and to keep this thread around. ... Private Const item1 = 0 ...
    (microsoft.public.vb.bugs)