BooleanSwitch.Enabled プロパティ

定義

スイッチが有効か無効かを示す値を取得または設定します。

public:
 property bool Enabled { bool get(); void set(bool value); };
public bool Enabled { get; set; }
member this.Enabled : bool with get, set
Public Property Enabled As Boolean

プロパティ値

true スイッチが有効になっている場合は、次の値を使用します。それ以外の場合は false。 既定値は false です。

例外

呼び出し元に正しいアクセス許可がありません。

次のコード例では、 BooleanSwitch を作成し、スイッチを使用してエラー メッセージを出力するかどうかを判断します。 スイッチはクラス レベルで作成されます。 Main メソッドは、その場所をMyMethodに渡し、エラー メッセージとエラーが発生した場所を出力します。

//Class level declaration.
/* Create a BooleanSwitch for data.*/
static BooleanSwitch dataSwitch = new BooleanSwitch("Data", "DataAccess module");

static public void MyMethod(string location)
{
    //Insert code here to handle processing.
    if (dataSwitch.Enabled)
        Console.WriteLine("Error happened at " + location);
}

public static void Main(string[] args)
{
    //Run the method that writes an error message specifying the location of the error.
    MyMethod("in Main");
}
'Class level declaration.
' Create a BooleanSwitch for data. 
Private Shared dataSwitch As New BooleanSwitch("Data", "DataAccess module")


Public Shared Sub MyMethod(location As String)
    'Insert code here to handle processing.
    If dataSwitch.Enabled Then
        Console.WriteLine(("Error happened at " + location))
    End If
End Sub

'Entry point which delegates to C-style main Private Function
Public Overloads Shared Sub Main()
    Main(System.Environment.GetCommandLineArgs())
End Sub
 
Overloads Public Shared Sub Main(args() As String)
    'Run the method that writes an error message specifying the location of the error.
    MyMethod("in Main")
End Sub

注釈

既定では、このフィールドは false (無効) に設定されています。 スイッチを有効にするには、このフィールドに trueの値を割り当てます。 スイッチを無効にするには、 falseに値を割り当てます。 このプロパティの値は、基底クラスのプロパティ SwitchSettingの値によって決まります。

適用対象

こちらもご覧ください