RE: passing enum value as an argument
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Tue, 12 Jul 2005 23:57:18 +0100
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
.
- Follow-Ups:
- RE: passing enum value as an argument
- From: Madestro
- RE: passing enum value as an argument
- References:
- passing enum value as an argument
- From: Glenn Venzke
- 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
- passing enum value as an argument
- Prev by Date: Re: Global Assembly Cache
- Next by Date: Re: MAPI and .Net - Microsoft says both supported and not?
- Previous by thread: RE: passing enum value as an argument
- Next by thread: RE: passing enum value as an argument
- Index(es):
Relevant Pages
|