RE: passing enum value as an argument



Madestro <me_no_like_spam_juanDOTromero@bowneDOTcom> wrote:
> That is exactly the point. I encourage Enums. Going back to the question, the
> user is trying to use them without fully qualifying them which in turn
> results in "defeat of the purpose" like I mentioned before.

But it *doesn't* defeat the purpose. It doesn't stop the compiler from
noticing if he's trying to pass in a value which isn't in the enum. It
doesn't stop the compiler from noticing if he's trying to pass in a
value from another enum, whether explicitly or not.

If you had two enums, FirstEnum with values Foo, Bar, Baz and
SecondEnum with values Fred, George, Harry, and two methods:

TakeFirstEnum (FirstEnum x)
TakeSecondEnum (SecondEnum y)

then the following would be valid calls:
TakeFirstEnum(Foo)
TakeFirstEnum(FirstEnum.Foo)
TakeSecondEnum(Fred)
TakeSecondEnum(SecondEnum.Fred)


and the following would be invalid calls:
TakeFirstEnum(Fred)
TakeFirstEnum(SecondEnum.Fred)
TakeSecondEnum(Foo)
TakeSecondEnum(FirstEnum.Foo)

So there's still just as much type safety there as there was before -
which would *not* be the case if he'd just gone with ints rather than
enums in the first place. For that reason, I fail to see how it defeats
the purpose.

The only times it would cause potential for confusion would be where
there was an overloaded method where the differing parameter types were
both enums. In that case, the compiler could force the developer to
explicitly state which enum he wanted to use.

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: Re: one more legal statistical licences will freely settle the favours
    ... We defeat them, then we away increase Ayman and ... Ayub's embarrassed accuracy. ... Otherwise the purpose in Usha's ...
    (sci.crypt)
  • Re: Connection to a MS SQL Server Table (newbie)
    ... > machines with IIS and MS SQL Server. ... The latter seems to defeat the purpose of a password. ... > What I am guessing at this point is that a SQL server account needs to be ...
    (microsoft.public.inetserver.asp.db)
  • Re: passing enum value as an argument
    ... Do you actually write much code without option strict on? ... about type safety. ... > You seem to think that the sole purpose of enums is to force the ... For you to get the benefits of an Enum, ...
    (microsoft.public.dotnet.general)
  • Re: Enums Type Question
    ... The purpose was to abstract the name the programmer would see when choosing ... would represent the real css class to be applied to the control when the ... well as the enum value. ... public void message(string msgStr, messageType msgType) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Websites known for malware
    ... some users are still on dial-up and regardless ... Posting a link does NOT defeat the purpose., well maybe to you but not ... > identified as being a malware rat?! ...
    (microsoft.public.security)