Updated: March 2009
Returns an indication whether a constant with a specified value exists in a specified enumeration.
Namespace:
System
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
<ComVisibleAttribute(True)> _
Public Shared Function IsDefined ( _
enumType As Type, _
value As Object _
) As Boolean
Dim enumType As Type
Dim value As Object
Dim returnValue As Boolean
returnValue = Enum.IsDefined(enumType, _
value)
[ComVisibleAttribute(true)]
public static bool IsDefined(
Type enumType,
Object value
)
[ComVisibleAttribute(true)]
public:
static bool IsDefined(
Type^ enumType,
Object^ value
)
public static function IsDefined(
enumType : Type,
value : Object
) : boolean
Return Value
Type:
System..::.Boolean
true if a constant in enumType has a value equal to value; otherwise, false.
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.
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 Main()
Dim value As Object
' Call IsDefined with underlying integral value of member.
value = 1
Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
' Call IsDefined with invalid underlying integral value.
value = 64
Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
' Call IsDefined with string containing member name.
value = "Rodent"
Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
' Call IsDefined with a variable of type PetType.
value = PetType.Dog
Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
value = PetType.Dog Or PetType.Cat
Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
' Call IsDefined with uppercase member name.
value = "None"
Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
value = "NONE"
Console.WriteLine("{0}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
' Call IsDefined with combined value
value = PetType.Dog Or PetType.Bird
Console.WriteLine("{0:D}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
value = value.ToString()
Console.WriteLine("{0:D}: {1}", value, [Enum].IsDefined(GetType(PetType), value))
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 Main()
{
object value;
// Call IsDefined with underlying integral value of member.
value = 1;
Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
// Call IsDefined with invalid underlying integral value.
value = 64;
Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
// Call IsDefined with string containing member name.
value = "Rodent";
Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
// Call IsDefined with a variable of type PetType.
value = PetType.Dog;
Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
value = PetType.Dog | PetType.Cat;
Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
// Call IsDefined with uppercase member name.
value = "None";
Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
value = "NONE";
Console.WriteLine("{0}: {1}", value, Enum.IsDefined(typeof(PetType), value));
// Call IsDefined with combined value
value = PetType.Dog | PetType.Bird;
Console.WriteLine("{0:D}: {1}", value, Enum.IsDefined(typeof(PetType), value));
value = value.ToString();
Console.WriteLine("{0:D}: {1}", 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
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 3.5, 2.0, 1.0
XNA Framework
Supported in: 3.0, 2.0, 1.0
Reference
Date | History | Reason |
|---|
March 2009
| Added detail to the Exceptions section. |
Customer feedback.
|
August 2008
| Added remarks and an example. |
Customer feedback.
|