RE: passing enum value as an argument



Madestro <me_no_like_spam_juanDOTromero@bowneDOTcom> wrote:
> I am not sure what language you are coding in

C#.

> but at least in VB.NET you are incorrect.

Not with option strict on. If you're programming with option strict
off, that's a different matter - you've given up any vestige of compile
time type safety already.

> Here are the TRUE results:
>
> TakeFirstEnum(Foo) --> FAILS
> TakeFirstEnum(FirstEnum.Foo) --> SUCCEEDS
> TakeSecondEnum(Fred) --> FAILS
> TakeSecondEnum(SecondEnum.Fred) --> SUCCEEDS
>
> TakeFirstEnum(Fred) --> FAILS
> TakeFirstEnum(SecondEnum.Fred) --> SUCCEEDS
> TakeSecondEnum(Foo) --> FAILS
> TakeSecondEnum(FirstEnum.Foo) --> SUCCEEDS

The failures are all because the language doesn't allow what's being
proposed, of course.

However, I don't get the same results as you. Here's a short but
complete program to demonstrate:

Option Strict On

Imports System

Public Enum FirstEnum
Foo
Bar
Baz
End Enum

Public Enum SecondEnum
Fred
George
Harry
End Enum


Public Class Test

Shared Sub TakeFirstEnum (value As FirstEnum)
Console.WriteLine (value)
End Sub

Shared Sub TakeSecondEnum (value As SecondEnum)
Console.WriteLine (value)
End Sub

Shared Sub Main()
TakeFirstEnum(FirstEnum.Foo)
TakeFirstEnum(SecondEnum.Fred)
TakeSecondEnum(FirstEnum.Foo)
TakeSecondEnum(SecondEnum.Fred)
End Sub
End Class

Compiling the above gives:
c:\test\Test.vb(30) : error BC30512: Option Strict On disallows
implicit conversions from 'SecondEnum' to 'FirstEnum'.

TakeFirstEnum(SecondEnum.Fred)
~~~~~~~~~~~~~~~
c:\test\Test.vb(31) : error BC30512: Option Strict On disallows
implicit conversions from 'FirstEnum' to 'SecondEnum'.

TakeSecondEnum(FirstEnum.Foo)
~~~~~~~~~~~~~

Turning option strict off, it compiles fine, but as I said, if you're
running with option strict off I have very little sympathy with any
type safety issues you may run into :)

> Like I said before, ultimately, the enum evaluates to a constant

Of the appropriate type though - the type of the expression is known to
the compiler.

> so there
> is really no "type safety" here, except for the type of the constant and the
> range of valid values the enum provides.

Actually, the range of valid values the enum provides *isn't* part of
the safety - you can cast any value from the underlying type.

> Because of this, I could call the first function with a value from
> the second Enum so long as it evaluates to a value within the range
> of the first Enum.

That's *entirely* wrong. With option strict on you can't call it at
all, and with option strict you can call it whether it's in the range
or not.

> What I cannot do however is call one of the values of the Enum
> without fully qualifying it because once again, like I said before)
> you would run into ambiguity problems.

Well, you can't do it at the moment because the language specification
says you can't. There's nothing to stop the language specification from
changing to allow you to do it where there was no ambiguity - and that
change *wouldn't* lose the whole point of enums.

> It may not be the case in the language you are using, but it certainly is
> the case with VB.NET. Try it, you will see.

I tried it, and found what you asserted to be wrong - did you actually
try it, or were you making assumptions?

--
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
    ... Option Strict before. ... and I still stand on my ground: Refering to Enum ... > | It may not be the case in the language you are using, ... > |> SecondEnum with values Fred, George, Harry, and two methods: ...
    (microsoft.public.dotnet.general)
  • RE: passing enum value as an argument
    ... > hours after your only previous reference to Option Strict? ... indeed you would have an Enum, and you WOULD have to use the same type on the ... the compiler would force you to ... >> Public sub New ...
    (microsoft.public.dotnet.general)
  • RE: passing enum value as an argument
    ... I did talk about Option Strict. ... > I clearly stated that you could CAST to the Enum type. ... > Public sub New ... > Juan Romero ...
    (microsoft.public.dotnet.general)
  • RE: passing enum value as an argument
    ... I did talk about Option Strict. ... I clearly stated that you could CAST to the Enum type. ... Public sub New ... a language can NEVER allow you to use the name ...
    (microsoft.public.dotnet.general)
  • Re: Odd string compare results
    ... > Option Explicit On ... > Option Strict On ... Class Test ... Shared Sub Main ...
    (microsoft.public.dotnet.general)