
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
30
|
Chapter 1: Numbers and Enumerations
MSIL = 0x0080,
All = (CSharp | VBNET | VB6 | Cpp | FortranNET | Jsharp | MSIL),
VBOnly = (VBNET | VB6),
NonVB = (CSharp | Cpp | FortranNET | Jsharp | MSIL)
}
Now you have two extra members in the enumerations, one that encompasses VB-
only languages (
Languages.VBNET and Languages.VB6) and one that encompasses non-
VB languages.
1.21 Determining if One or More Enumeration Flags
Are Set
Problem
You need to determine if a variable of an enumeration type, consisting of bit flags,
contains one or more specific flags. For example, given the following enumeration
Language:
[Flags]
enum Language
{
CSharp = 0x0001, VBNET = 0x0002, VB6 = 0x0004, Cpp = 0x0008
}
determine, using Boolean logic, if the variable lang in the following line of code con-
tains a language such as
Language.CSharp and/or Language.Cpp:
Language lang = Language.CSharp | Language.VBNET;
Solution
To determine if a variable contains a single bit flag that is set, use the following
conditional:
if((lang & Language.CSharp) == Language.CSharp)
{
// Lang contains at least Language.CSharp.
}
To determine if a variable exclusively contains a single bit flag that is set, use the fol-
lowing conditional:
if(lang == Language.CSharp)
{
// lang contains only the Language.CSharp.
}
To determine if a variable contains ...