FlagsAttribute クラスとは? わかりやすく解説

FlagsAttribute クラス

列挙体をビット フィールド、つまりフラグセットとして扱えることを示します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
<AttributeUsageAttribute(AttributeTargets.Enum, Inherited:=False)> _
<ComVisibleAttribute(True)> _
Public Class FlagsAttribute
    Inherits Attribute
Dim instance As FlagsAttribute
[SerializableAttribute] 
[AttributeUsageAttribute(AttributeTargets.Enum, Inherited=false)]
 
[ComVisibleAttribute(true)] 
public class FlagsAttribute : Attribute
[SerializableAttribute] 
[AttributeUsageAttribute(AttributeTargets::Enum, Inherited=false)]
 
[ComVisibleAttribute(true)] 
public ref class FlagsAttribute : public
 Attribute
/** @attribute SerializableAttribute() */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Enum, Inherited=false)
 */ 
/** @attribute ComVisibleAttribute(true) */ 
public class FlagsAttribute extends Attribute
SerializableAttribute 
AttributeUsageAttribute(AttributeTargets.Enum, Inherited=false)
 
ComVisibleAttribute(true) 
public class FlagsAttribute extends
 Attribute
解説解説

通常ビット フィールドは、組み合わせて使用できる要素リストとして使用されます。一方列挙定数は、同時に選択できない要素リストとして使用されます。つまり、ビット フィールドは、ビットごとの OR 演算組み合わせて無名の値を作成できるような設計になっていますが、列挙定数そのように設計されていません。言語によって、列挙定数ビット フィールド使用方法違いはさまざまです。

FlagsAttribute の属性

AttributeUsageAttribute はこのクラス適用されます。また、その Inherited プロパティfalse指定します。この属性適用できるのは列挙体だけです。

FlagsAttribute および Enumガイドライン
使用例使用例

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
*/
継承階層継承階層
System.Object
   System.Attribute
    System.FlagsAttribute
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「FlagsAttribute クラス」の関連用語

FlagsAttribute クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



FlagsAttribute クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS