Export (0) Print
Expand All

Enum.Format Method

Converts the specified value of a specified enumerated type to its equivalent string representation according to the specified format.

[Visual Basic]
Public Shared Function Format( _
   ByVal enumType As Type, _
   ByVal value As Object, _
   ByVal format As String _
) As String
[C#]
public static string Format(
 Type enumType,
 object value,
 string format
);
[C++]
public: static String* Format(
 Type* enumType,
 Object* value,
 String* format
);
[JScript]
public static function Format(
   enumType : Type,
 value : Object,
 format : String
) : String;

Parameters

enumType
The enumeration type of the value to convert.
value
The value to convert.
format
The output format to use.

Return Value

A string representation of value.

Exceptions

Exception Type Condition
ArgumentNullException The enumType, value, or format parameter is a null reference (Nothing in Visual Basic).
ArgumentException The enumType parameter is not an Enum type.

-or-

The value is from an enumeration that differs in type from enumType.

-or-

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

FormatException The format parameter contains an invalid value.

Remarks

The valid format values are:

Format Description
"G" or "g" If value is equal to a named enumerated constant, the name of that constant is returned; otherwise, the decimal equivalent of value is returned.

For example, suppose the only enumerated constant is named, Red, and its value is 1. If value is specified as 1, then this format returns "Red". However, if value is specified as 2, this format returns "2".

-or-

If the FlagsAttribute custom attribute is applied to the enumeration, then value is treated as a bit field that contains one or more flags that consist of one or more bits.

If value is equal to a combination of named enumerated constants, a delimiter-separated list of the names of those constants is returned. value is searched for flags, going from the flag with the largest value to the smallest value. For each flag that corresponds to a bit field in value, the name of the constant is concatenated to the delimiter-separated list. Then the value of that flag is excluded from further consideration, and the search continues for the next flag.

If value is not equal to a combination of named enumerated constants, the decimal equivalent of value is returned.

"X" or "x" Represents value in hexadecimal without a leading "0x".
"D" or "d" Represents value in decimal form.
"F" or "f" Behaves identically to "G" or "g", except the FlagsAttribute is not required to be present on the Enum declaration.

Example

The following code sample illustrates the use of Format in the context of Enum:

[Visual Basic] 
Imports System

Public Class FormatTest
    
    Enum Colors
        Red
        Green
        Blue
        Yellow    
    End Enum 'Colors
    
    Public Shared Sub Main()
        Dim myColor As Colors = Colors.Blue
        
        Console.WriteLine("My favorite color is {0}", myColor)
        Console.WriteLine("The value of my favorite color is {0}", [Enum].Format(GetType(Colors), myColor, "d"))
        Console.WriteLine("The hex value of my favorite color is {0}", [Enum].Format(GetType(Colors), myColor, "x"))
    End Sub 'Main
End Class 'FormatTest

[C#] 
using System;

public class FormatTest {
    enum Colors { Red, Green, Blue, Yellow };

    public static void Main() {
        Colors myColor = Colors.Blue;

        Console.WriteLine("My favorite color is {0}", myColor);
        Console.WriteLine("The value of my favorite color is {0}", Enum.Format(typeof(Colors), myColor, "d"));
        Console.WriteLine("The hex value of my favorite color is {0}", Enum.Format(typeof(Colors), myColor, "x"));
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;

__value enum Colors { Red, Green, Blue, Yellow };

int main() {
    Colors myColor = Colors::Blue;
    Console::WriteLine("My favorite color is {0}", __box(myColor));
    Console::WriteLine("The value of my favorite color is {0}", Enum::Format(__typeof(Colors), __box(myColor), "d"));
    Console::WriteLine("The hex value of my favorite color is {0}", Enum::Format(__typeof(Colors), __box(myColor), "x"));

}

[JScript] 
import System;

public class FormatTest {
    enum Colors { Red, Green, Blue, Yellow };

    public static function Main() {
        var myColor : Colors = Colors.Blue;

        Console.WriteLine("My favorite color is {0}", myColor);
        Console.WriteLine("The value of my favorite color is {0}", Enum.Format(Colors, myColor, "d"));
        Console.WriteLine("The hex value of my favorite color is {0}", Enum.Format(Colors, myColor, "x"));
    }
}

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

Enum Class | Enum Members | System Namespace | Formatting Overview | Enumeration Format Strings | ToString

Show:
© 2015 Microsoft