FlagsAttribute コンストラクタ
アセンブリ: mscorlib (mscorlib.dll 内)
構文
使用例
FlagsAttribute 属性を Enum 宣言で使用した場合の ToString メソッドへの影響を示すコード例を次に示します。
' Example of the FlagsAttribute attribute. Imports System Imports Microsoft.VisualBasic Module FlagsAttributeDemo ' Define an Enum without FlagsAttribute. Enum SingleHue as Short Black = 0 Red = 1 Green = 2 Blue = 4 End Enum ' Define an Enum with FlagsAttribute. <FlagsAttribute( )> _ Enum MultiHue as Short Black = 0 Red = 1 Green = 2 Blue = 4 End Enum Sub Main( ) Console.WriteLine( _ "This example of the FlagsAttribute attribute " & _ vbCrLf & "generates the following output." ) Console.WriteLine( vbCrLf & _ "All possible combinations of values of an " & _ vbCrLf & "Enum without FlagsAttribute:" & vbCrLf ) ' Display all possible combinations of values. Dim val as Integer For val = 0 to 8 Console.WriteLine( "{0,3} - {1}", _ val, CType( val, SingleHue ).ToString( ) ) Next val Console.WriteLine( vbCrLf & _ "All possible combinations of values of an " & _ vbCrLf & "Enum with FlagsAttribute:" & vbCrLf ) ' Display all possible combinations of values. ' Also display an invalid value. For val = 0 to 8 Console.WriteLine( "{0,3} - {1}", _ val, CType( val, MultiHue ).ToString( ) ) Next val End Sub End Module ' This example of the FlagsAttribute attribute ' generates the following output. ' ' All possible combinations of values of an ' Enum without FlagsAttribute: ' ' 0 - Black ' 1 - Red ' 2 - Green ' 3 - 3 ' 4 - Blue ' 5 - 5 ' 6 - 6 ' 7 - 7 ' 8 - 8 ' ' All possible combinations of values of an ' Enum with FlagsAttribute: ' ' 0 - Black ' 1 - Red ' 2 - Green ' 3 - Red, Green ' 4 - Blue ' 5 - Red, Blue ' 6 - Green, Blue ' 7 - Red, Green, Blue ' 8 - 8
// Example of the FlagsAttribute attribute. using System; class FlagsAttributeDemo { // Define an Enum without FlagsAttribute. enum SingleHue : short { Black = 0, Red = 1, Green = 2, Blue = 4 }; // Define an Enum with FlagsAttribute. [FlagsAttribute] enum MultiHue : short { Black = 0, Red = 1, Green = 2, Blue = 4 }; static void Main( ) { Console.WriteLine( "This example of the FlagsAttribute attribute \n" + "generates the following output." ); Console.WriteLine( "\nAll possible combinations of values of an \n" + "Enum without FlagsAttribute:\n" ); // Display all possible combinations of values. for( int val = 0; val <= 8; val++ ) Console.WriteLine( "{0,3} - {1}", val, ( (SingleHue)val ).ToString( ) ); Console.WriteLine( "\nAll possible combinations of values of an \n" + "Enum with FlagsAttribute:\n" ); // Display all possible combinations of values. // Also display an invalid value. for( int val = 0; val <= 8; val++ ) Console.WriteLine( "{0,3} - {1}", val, ( (MultiHue)val ).ToString( ) ); } } /* This example of the FlagsAttribute attribute generates the following output. All possible combinations of values of an Enum without FlagsAttribute: 0 - Black 1 - Red 2 - Green 3 - 3 4 - Blue 5 - 5 6 - 6 7 - 7 8 - 8 All possible combinations of values of an Enum with FlagsAttribute: 0 - Black 1 - Red 2 - Green 3 - Red, Green 4 - Blue 5 - Red, Blue 6 - Green, Blue 7 - Red, Green, Blue 8 - 8 */
// Example of the FlagsAttribute attribute. using namespace System; // Define an Enum without FlagsAttribute. enum class SingleHue : short { Black = 0, Red = 1, Green = 2, Blue = 4 }; // Define an Enum with FlagsAttribute. [FlagsAttribute] enum class MultiHue : short { Black = 0, Red = 1, Green = 2, Blue = 4 }; int main() { Console::WriteLine( "This example of the FlagsAttribute attribute \n" "generates the following output." ); Console::WriteLine( "\nAll possible combinations of values of an \n" "Enum without FlagsAttribute:\n" ); // Display all possible combinations of values. for ( int val = 0; val <= 8; val++ ) Console::WriteLine( "{0,3} - {1}", val, ((SingleHue)val).ToString() ); Console::WriteLine( "\nAll possible combinations of values of an \n" "Enum with FlagsAttribute:\n" ); // Display all possible combinations of values. // Also display an invalid value. for ( int val = 0; val <= 8; val++ ) Console::WriteLine( "{0,3} - {1}", val, ((MultiHue)val).ToString() ); } /* This example of the FlagsAttribute attribute generates the following output. All possible combinations of values of an Enum without FlagsAttribute: 0 - Black 1 - Red 2 - Green 3 - 3 4 - Blue 5 - 5 6 - 6 7 - 7 8 - 8 All possible combinations of values of an Enum with FlagsAttribute: 0 - Black 1 - Red 2 - Green 3 - Red, Green 4 - Blue 5 - Red, Blue 6 - Green, Blue 7 - Red, Green, Blue 8 - 8 */
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- FlagsAttribute コンストラクタのページへのリンク