Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Using enum as a parameter
Message
De
22/12/2004 11:21:21
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
00971423
Message ID:
00971455
Vues:
17
>I tried using an enum as a parameter and it worked well but not quite like I expected.
>Consider the sample code:
>
>public enum myEnum : byte
>{
>  SR0 = 0xE0,
>  SR1 = 0xE1,
>  SR2 = 0xE2
>}
>
>public static void myMethod (myEnum paraMyEnum)
>{
>  MessageBox.Show(((byte) paraMyEnum).ToString());
>}
>
>
>Now I would this that I could only call my myMethod passing only one of these three parameters: myEnum.SR0, myEnum.SR1 or myEnum.SR2 but it appears I can call myMethod with any byte as a parameter (and even something that can be converted to byte without loss of accuracy like an int).
>
>So is there any way, other than checking if the passed parameter is contained in the enum, to ensure that I am only passed a certain parameter?
>
>Einar

Einar,
By definition (From "C# Language specification"):
"The set of values that an enum type can take on is not limited by its enum members. In particular, any value of the underlying type of an enum can be cast to the enum type, and is a distinct valid value of that enum type."

You've to check what you get.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform