Enum.HasFlag Method
Determines whether one or more bit fields are set in the current instance.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
'Declaration <SecuritySafeCriticalAttribute> _ Public Function HasFlag ( _ flag As Enum _ ) As Boolean
Parameters
- flag
- Type: System.Enum
An enumeration value.
Return Value
Type: System.Booleantrue if the bit field or bit fields that are set in flag are also set in the current instance; otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentException | flag is a different type than the current instance. |
The HasFlag method returns the result of the following Boolean expression.
thisInstance And flag = flag
If the underlying value of flag is zero, the method returns true. If this behavior is not desirable, you can use the Equals method to test for equality with zero and call HasFlag only if the underlying value of flag is non-zero, as the following example illustrates.
<Flags()> Public Enum Pet None = 0 Dog = 1 Cat = 2 Bird = 4 Rabbit = 8 Other = 16 End Enum Module Example Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) Dim petsInFamilies() As Pet = {Pet.None, Pet.Dog Or Pet.Cat, Pet.Dog} Dim familiesWithoutPets As Integer Dim familiesWithDog As Integer For Each petsInFamily In petsInFamilies ' Count the number of families without pets. If petsInFamily.Equals(Pet.None) Then familiesWithoutPets += 1 ' Of families with a pet, count the number of families with a dog. Else If petsInFamily.HasFlag(Pet.Dog) Then familiesWithDog += 1 End If Next outputBlock.Text += String.Format("{0} of {1} families in the sample have no pets." & vbCrLf, _ familiesWithDog, petsInFamilies.Length) outputBlock.Text += String.Format("{0} of {1} families in the sample have a dog." & vbCrLf, _ familiesWithDog, petsInFamilies.Length) End Sub End Module ' The example displays the following output: ' 1 of 3 families in the sample have no pets. ' 2 of 3 families in the sample have a dog.
The HasFlag method is designed to be used with enumeration types that are marked with the FlagsAttribute attribute. For enumeration types that are not marked with the FlagsAttribute attribute, call either the Equals method or the CompareTo method.
The following example defines an ItemsOrdered enumeration that reflects categories of items that a customer can order in a restaurant. The example tests whether the customer has ordered both an entrée and a beverage.
<Flags()> Public Enum DinnerItems As Integer None = 0 Entree = 1 Appetizer = 2 Side = 4 Dessert = 8 Beverage = 16 BarBeverage = 32 End Enum Module Example Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) Dim myOrder As DinnerItems = DinnerItems.Appetizer Or DinnerItems.Entree Or _ DinnerItems.Beverage Or DinnerItems.Dessert Dim flagValue As DinnerItems = DinnerItems.Entree Or DinnerItems.Beverage outputBlock.Text += String.Format("{0} includes {1}: {2}" & vbCrLf, _ myOrder, flagValue, myOrder.HasFlag(flagValue)) End Sub End Module ' The example displays the following output: ' Entree, Appetizer, Dessert, Beverage includes Entree, Beverage: True
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.