다음을 통해 공유


Enum.Format 메서드

열거 형식이 지정된 특정 값을 특정 형식에 따라 해당 문자열 표현으로 변환합니다.

네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
<ComVisibleAttribute(True)> _
Public Shared Function Format ( _
    enumType As Type, _
    value As Object, _
    format As String _
) As String
‘사용 방법
Dim enumType As Type
Dim value As Object
Dim format As String
Dim returnValue As String

returnValue = Enum.Format(enumType, value, format)
[ComVisibleAttribute(true)] 
public static string Format (
    Type enumType,
    Object value,
    string format
)
[ComVisibleAttribute(true)] 
public:
static String^ Format (
    Type^ enumType, 
    Object^ value, 
    String^ format
)
/** @attribute ComVisibleAttribute(true) */ 
public static String Format (
    Type enumType, 
    Object value, 
    String format
)
ComVisibleAttribute(true) 
public static function Format (
    enumType : Type, 
    value : Object, 
    format : String
) : String

매개 변수

  • enumType
    변환할 값의 열거형 형식입니다.
  • value
    변환할 값입니다.
  • format
    사용할 출력 형식입니다.

반환 값

value의 문자열 표현입니다.

예외

예외 형식 조건

ArgumentNullException

enumType, value 또는 format 매개 변수가 Null 참조(Visual Basic의 경우 Nothing)인 경우

ArgumentException

enumType 매개 변수가 Enum 형식이 아닌 경우

- 또는 -

value가 enumType과 형식이 다른 열거형인 경우

- 또는 -

value의 형식이 enumType의 내부 형식이 아닌 경우

FormatException

format 매개 변수 값이 잘못된 경우

설명

유효한 형식 값은 다음과 같습니다.

형식

설명

"G" 또는 "g"

value가 명명된 열거 상수와 같으면 해당 상수의 이름이 반환되고, 그렇지 않으면 value에 해당하는 10진수가 반환됩니다.

예를 들어, 값이 1인 Red라는 열거 상수만 있는 경우 value가 1로 지정되면 이 형식은 "Red"를 반환합니다. 그러나 value가 2로 지정되면 이 형식은 "2"를 반환합니다.

- 또는 -

FlagsAttribute 사용자 지정 특성이 열거형에 적용되면 value는 하나 이상의 비트로 구성된 하나 이상의 플래그를 포함하는 비트 필드로 처리됩니다.

value가 명명된 열거 상수를 조합한 것과 같으면 구분 기호로 구분된 상수 이름 목록이 반환됩니다. value는 가장 큰 값에서 가장 작은 값 순서로 플래그를 검색합니다. value의 비트 필드에 해당하는 각 플래그에 대해 상수의 이름은 구분 기호로 구분된 목록에 연결됩니다. 해당 플래그의 값은 더 이상 고려되지 않으며, 다음 플래그가 계속 검색됩니다.

value가 명명된 열거 상수의 조합과 다르면 value에 해당하는 10진수가 반환됩니다.

"X" 또는 "x"

"0x"가 앞에 오지 않는 16진수의 value를 나타냅니다.

"D" 또는 "d"

value를 10진수 형식으로 나타냅니다.

"F" 또는 "f"

FlagsAttributeEnum 선언에 있을 필요가 없다는 점을 제외하고는 "G" 또는 "g"와 동일하게 동작합니다.

예제

다음 코드 예제에서는 Enum 컨텍스트에서 Format을 사용합니다.

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
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"));
    }
}
using namespace System;
enum class Colors
{
   Red, Green, Blue, Yellow
};

int 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( Colors::typeid, myColor,  "d" ) );
   Console::WriteLine(  "The hex value of my favorite color is {0}", Enum::Format( Colors::typeid, myColor,  "x" ) );
}
import System.*;

public class FormatTest
{
    enum Colors
    {
        red (0),
        green (1),
        blue (2),
        yellow (3);
    } //Colors

    public static void main(String[] args)
    {
        Colors myColor = Colors.blue;

        Console.WriteLine("My favorite color is {0}", myColor);
        Console.WriteLine("The value of my favorite color is {0}", 
            Enum.Format(Colors.class.ToType(), myColor, "d"));
        Console.WriteLine("The hex value of my favorite color is {0}", 
            Enum.Format(Colors.class.ToType(), myColor, "x"));
    } //main
} //FormatTest
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"));
    }
}

플랫폼

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

참고 항목

참조

Enum 구조체
Enum 멤버
System 네임스페이스
ToString

기타 리소스

형식 지정 개요
열거형 형식 문자열