Edit

Share via


BooleanSwitch Constructors

Definition

Initializes a new instance of the BooleanSwitch class.

Overloads

BooleanSwitch(String, String)

Initializes a new instance of the BooleanSwitch class with the specified display name and description.

BooleanSwitch(String, String, String)

Initializes a new instance of the BooleanSwitch class with the specified display name, description, and default switch value.

BooleanSwitch(String, String)

Source:
BooleanSwitch.cs
Source:
BooleanSwitch.cs
Source:
BooleanSwitch.cs

Initializes a new instance of the BooleanSwitch class with the specified display name and description.

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)

Parameters

displayName
String

The name to display on a user interface.

description
String

The description of the switch.

Examples

The following example creates a BooleanSwitch and uses the switch to determine whether to print an error message. The switch is created at the class level. The Main method passes its location to MyMethod, which prints an error message and where the error occurred.

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

Remarks

When you create a BooleanSwitch, the displayName parameter is used to find the initial switch settings for .NET Framework apps in the application configuration file. If the constructor cannot find initial settings, or for .NET Core and .NET 5+ apps, the Enabled property is set to false (disabled).

To set the level of your BooleanSwitch in a .NET Framework app, edit the configuration file corresponding to the name of your application. Within this file, you can add a switch and set its value, remove a switch, or clear all switches previously set by the application. The configuration file should be formatted like the following example:

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

Note

The switches you created should be static.

See also

Applies to

BooleanSwitch(String, String, String)

Source:
BooleanSwitch.cs
Source:
BooleanSwitch.cs
Source:
BooleanSwitch.cs

Initializes a new instance of the BooleanSwitch class with the specified display name, description, and default switch value.

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)

Parameters

displayName
String

The name to display on the user interface.

description
String

The description of the switch.

defaultSwitchValue
String

The default value of the switch.

Applies to