BooleanSwitch 建構函式

定義

初始化 BooleanSwitch 類別的新執行個體。

多載

BooleanSwitch(String, String)

使用指定的顯示名稱和描述,初始化 BooleanSwitch 類別的新執行個體。

BooleanSwitch(String, String, String)

以指定的顯示名稱、描述和預設參數值初始化 BooleanSwitch 類別的新執行個體。

BooleanSwitch(String, String)

來源:
BooleanSwitch.cs
來源:
BooleanSwitch.cs
來源:
BooleanSwitch.cs

使用指定的顯示名稱和描述,初始化 BooleanSwitch 類別的新執行個體。

public:
 BooleanSwitch(System::String ^ displayName, System::String ^ description);
public BooleanSwitch (string displayName, string? description);
public BooleanSwitch (string displayName, string description);
new System.Diagnostics.BooleanSwitch : string * string -> System.Diagnostics.BooleanSwitch
Public Sub New (displayName As String, description As String)

參數

displayName
String

要顯示在使用者介面上的名稱。

description
String

參數的描述。

範例

下列範例會 BooleanSwitch 建立 ,並使用 參數來判斷是否要列印錯誤訊息。 參數會在類別層級建立。 方法 Main 會將其位置傳遞至 MyMethod,這會列印錯誤訊息,以及發生錯誤的位置。

public ref class BooleanSwitchTest
{
private:

   /* Create a BooleanSwitch for data.*/
   static BooleanSwitch^ dataSwitch = gcnew BooleanSwitch( "Data","DataAccess module" );

public:
   static void MyMethod( String^ location )
   {
      
      //Insert code here to handle processing.
      if ( dataSwitch->Enabled )
            Console::WriteLine( "Error happened at {0}", location );
   }

};

int main()
{
   
   //Run the method which writes an error message specifying the location of the error.
   BooleanSwitchTest::MyMethod( "in main" );
}
// 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 which 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 function.
Public Overloads Shared Sub Main()
    Main(System.Environment.GetCommandLineArgs())
End Sub
 
Overloads Public Shared Sub Main(args() As String)
    ' Run the method which writes an error message specifying the location of the error.
    MyMethod("in Main")
End Sub

備註

當您建立 BooleanSwitch時,displayName參數會用來尋找應用程式組態檔中 .NET Framework 應用程式的初始參數設定。 如果建構函式找不到初始設定,或 .NET Core 和 .NET 5+ 應用程式,屬性 Enabled 會設定為 false (停用) 。

若要在 .NET Framework 應用程式中設定層級BooleanSwitch,請編輯對應至應用程式名稱的組態檔。 在此檔案中,您可以新增參數並設定其值、移除參數,或清除應用程式先前設定的所有參數。 組態檔的格式應該如下列範例所示:

<configuration>  
    <system.diagnostics>  
       <switches>  
          <add name="mySwitch" value="10" />  
          <add name="myNewSwitch" value="20" />  
          <remove name="mySwitch" />  
          <clear/>  
       </switches>  
    </system.diagnostics>  
 </configuration>  

注意

您建立的參數應該是 static

另請參閱

適用於

BooleanSwitch(String, String, String)

來源:
BooleanSwitch.cs
來源:
BooleanSwitch.cs
來源:
BooleanSwitch.cs

以指定的顯示名稱、描述和預設參數值初始化 BooleanSwitch 類別的新執行個體。

public:
 BooleanSwitch(System::String ^ displayName, System::String ^ description, System::String ^ defaultSwitchValue);
public BooleanSwitch (string displayName, string? description, string defaultSwitchValue);
public BooleanSwitch (string displayName, string description, string defaultSwitchValue);
new System.Diagnostics.BooleanSwitch : string * string * string -> System.Diagnostics.BooleanSwitch
Public Sub New (displayName As String, description As String, defaultSwitchValue As String)

參數

displayName
String

要在使用者介面上顯示的名稱。

description
String

參數的描述。

defaultSwitchValue
String

參數的預設值。

適用於