Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Flag enum
Message
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Versions des environnements
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Divers
Thread ID:
01269641
Message ID:
01269745
Vues:
9
>Hi,
>I am using [flag] enum to store combination of multiple value. How could I test if it contains certain value?
>
>Thank you

(you may know this, but just in case you don't) the [Flag] attribute isn't enough to make an enum addable (it changes the behavior of ToString()). You need to explicitly set the values, ex.
public enum Sample
{
   OptionOne = 1,
   OptionTwo = 2, 
   OptionThree = 4,
   OptionFour = 8
}
To determine which flags are set:
Sample sampleFlag = Sample.OptionTwo | Sample.OptionThree

if ((sampleFlag & Sample.OptionTwo) == Sample.OptionTwo)
   // Do something, OptionTwo is selected

if ((sampleFlag & Sample.OptionThree) == Sample.OptionThree)
   // Do something, OptionThree is selected
-Paul

RCS Solutions, Inc.
Blog
Twitter
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform