Поделиться через


Enum.IsDefined Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns an indication whether a constant with a specified value exists in a specified enumeration.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Shared Function IsDefined ( _
    enumType As Type, _
    value As Object _
) As Boolean
[ComVisibleAttribute(true)]
public static bool IsDefined(
    Type enumType,
    Object value
)

Parameters

  • value
    Type: System.Object
    The value or name of a constant in enumType.

Return Value

Type: System.Boolean
true if a constant in enumType has a value equal to value; otherwise, false.

Exceptions

Exception Condition
ArgumentNullException

enumType or value is nulla null reference (Nothing in Visual Basic).

ArgumentException

enumType is not an Enum.

-or-

The type of value is not an enumType.

-or-

The type of value is not an underlying type of enumType.

InvalidOperationException

value is not type SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, or UInt64, or String.

Remarks

The value parameter can be any of the following:

  • Any member of type enumType.

  • A variable whose value is an enumeration member of type enumType.

  • The string representation of the name of an enumeration member. The characters in the string must have the same case as the enumeration member name.

  • A value of the underlying type of enumType.

If the constants in enumType define a set of bit fields and value contains the values, names, or underlying values of multiple bit fields, the IsDefined method returns false. In other words, for enumerations that define a set of bit fields, the method is designed to determine only whether a single bit field belongs to the enumeration.

Notes to Callers

If enumType is an enumeration that is defined by using the FlagsAttribute attribute, the method returns false if multiple bit fields in value are set but value does not correspond to a composite enumeration value, or if value is a string concatenation of the names of multiple bit flags.

Examples

The following example defines an enumeration named PetType that consists of individual bit fields. It then calls the IsDefined method with possible underlying enumeration values, string names, and composite values that result from setting multiple bit fields.

<Flags> Public Enum PetType As Integer
   None = 0
   Dog = 1
   Cat = 2
   Rodent = 4
   Bird = 8
   Reptile = 16
   Other = 32
End Enum

Module Example
   Public Sub Demo(outputBlock As System.Windows.Controls.TextBlock)
      Dim value As Object

      ' Call IsDefined with underlying integral value of member.
      value = 1
      outputBlock.Text += String.Format("{0}: {1}", _
                          value, [Enum].IsDefined(GetType(PetType), value)) + vbCrLf
      ' Call IsDefined with invalid underlying integral value.
      value = 64
      outputBlock.Text += String.Format("{0}: {1}", _
                          value, [Enum].IsDefined(GetType(PetType), value)) + vbCrLf
      ' Call IsDefined with string containing member name.
      value = "Rodent"
      outputBlock.Text += String.Format("{0}: {1}", _
                          value, [Enum].IsDefined(GetType(PetType), value)) + vbCrLf
      ' Call IsDefined with a variable of type PetType.
      value = PetType.Dog
      outputBlock.Text += String.Format("{0}: {1}", _
                          value, [Enum].IsDefined(GetType(PetType), value)) + vbCrLf
      value = PetType.Dog Or PetType.Cat
      outputBlock.Text += String.Format("{0}: {1}", _
                          value, [Enum].IsDefined(GetType(PetType), value)) + vbCrLf
      ' Call IsDefined with uppercase member name.      
      value = "None"
      outputBlock.Text += String.Format("{0}: {1}", _
                          value, [Enum].IsDefined(GetType(PetType), value)) + vbCrLf 
      value = "NONE"
      outputBlock.Text += String.Format("{0}: {1}", _
                          value, [Enum].IsDefined(GetType(PetType), value)) + vbCrLf
      ' Call IsDefined with combined value
      value = PetType.Dog Or PetType.Bird
      outputBlock.Text += String.Format("{0:D}: {1}", _
                          value, [Enum].IsDefined(GetType(PetType), value)) + vbCrLf
      value = value.ToString()
      outputBlock.Text += String.Format("{0:D}: {1}", _
                          value, [Enum].IsDefined(GetType(PetType), value)) + vbCrLf
   End Sub
End Module
' The example displays the following output:
'       1: True
'       64: False 
'       Rodent: True
'       Dog: True
'       Dog, Cat: False
'       None: True
'       NONE: False
'       9: False
'       Dog, Bird: False
using System;

[Flags] public enum PetType
{
   None = 0, Dog = 1, Cat = 2, Rodent = 4, Bird = 8, Reptile = 16, Other = 32
};

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      object value; 

      // Call IsDefined with underlying integral value of member.
      value = 1;
      outputBlock.Text += String.Format("{0}: {1}\n", 
                          value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with invalid underlying integral value.
      value = 64;
      outputBlock.Text += String.Format("{0}: {1}\n", 
                          value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with string containing member name.
      value = "Rodent";
      outputBlock.Text += String.Format("{0}: {1}\n", 
                          value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with a variable of type PetType.
      value = PetType.Dog;
      outputBlock.Text += String.Format("{0}: {1}\n", 
                          value, Enum.IsDefined(typeof(PetType), value));
      value = PetType.Dog | PetType.Cat;
      outputBlock.Text += String.Format("{0}: {1}\n", 
                          value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with uppercase member name.      
      value = "None";
      outputBlock.Text += String.Format("{0}: {1}\n", 
                          value, Enum.IsDefined(typeof(PetType), value));
      value = "NONE";
      outputBlock.Text += String.Format("{0}: {1}\n", 
                          value, Enum.IsDefined(typeof(PetType), value));
      // Call IsDefined with combined value
      value = PetType.Dog | PetType.Bird;
      outputBlock.Text += String.Format("{0:D}: {1}\n", 
                          value, Enum.IsDefined(typeof(PetType), value));
      value = value.ToString();
      outputBlock.Text += String.Format("{0:D}: {1}\n", 
                          value, Enum.IsDefined(typeof(PetType), value));
   }
}
// The example displays the following output:
//       1: True
//       64: False
//       Rodent: True
//       Dog: True
//       Dog, Cat: False
//       None: True
//       NONE: False
//       9: False
//       Dog, Bird: False

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference