- Defining the XML Switch
- Adding the BooleanSwitch to Code
- Managing Debug Code with the BooleanSwitch
- Summary
Adding the BooleanSwitch to Code
The role of BooleanSwitch is sentinel. There is only one switch value per defined switch; thus, you need to create only one BooleanSwitch instance, making a static field suitable for the BooleanSwitch instance.
The BooleanSwitch class is defined in the System.Diagnostics namespace. Create a static instance of a BooleanSwitch, passing the name of the switchthe name attribute for the <add> element, see line 5 of Listing 1and a description to the BooleanSwitch constructor. Then, evaluate the Enabled property. Place any amount of conditional code behind the BooleanSwitch.
Listing 2 demonstrates a basic example that displays one message if the switch is enabled and a second if the switch is disabled. To change the switch state, modify the value attribute (see line 5) of the switch definition and restart the application. You do not need to recompile.
Listing 2: A Windows Forms Example that Consumes the Switch Defined in Listing 1.
1: using System; 2: using System.Drawing; 3: using System.Collections; 4: using System.ComponentModel; 5: using System.Windows.Forms; 6: using System.Data; 7: using System.Diagnostics; 8: 9: namespace HasBooleanSwitch 10: { 11: /// <summary> 12: /// Summary description for Form1. 13: /// </summary> 14: public class Form1 : System.Windows.Forms.Form 15: { 16: private System.Windows.Forms.Button button1; 17: /// <summary> 18: /// Required designer variable. 19: /// </summary> 20: private System.ComponentModel.Container components = null; 21: 22: public Form1() 23: { 24: // 25: // Required for Windows Form Designer support 26: // 27: InitializeComponent(); 28: 29: // 30: // TODO: Add any constructor code after InitializeComponent call 31: // 32: } 33: 34: /// <summary> 35: /// Clean up any resources being used. 36: /// </summary> 37: protected override void Dispose( bool disposing ) 38: { 39: if( disposing ) 40: { 41: if (components != null) 42: { 43: components.Dispose(); 44: } 45: } 46: base.Dispose( disposing ); 47: } 48: 49: #region Windows Form Designer generated code 50: /// <summary> 51: /// Required method for Designer support - do not modify 52: /// the contents of this method with the code editor. 53: /// </summary> 54: private void InitializeComponent() 55: { 56: this.button1 = new System.Windows.Forms.Button(); 57: this.SuspendLayout(); 58: // 59: // button1 60: // 61: this.button1.Location = new System.Drawing.Point(128, 128); 62: this.button1.Name = "button1"; 63: this.button1.Size = new System.Drawing.Size(176, 48); 64: this.button1.TabIndex = 0; 65: this.button1.Text = "Click Me!"; 66: this.button1.Click += new System.EventHandler(this.button1_Click); 67: // 68: // Form1 69: // 70: this.AutoScaleBaseSize = new System.Drawing.Size(6, 15); 71: this.ClientSize = new System.Drawing.Size(432, 416); 72: this.Controls.AddRange(new System.Windows.Forms.Control[] { 73: this.button1}); 74: this.Name = "Form1"; 75: this.Text = "BooleanSwitch Example"; 76: this.ResumeLayout(false); 77: 78: } 79: #endregion 80: 81: /// <summary> 82: /// The main entry point for the application. 83: /// </summary> 84: [STAThread] 85: static void Main() 86: { 87: Application.Run(new Form1()); 88: } 89: 90: private static BooleanSwitch mySwitch = 91: new BooleanSwitch("MySwitch", "InformIT Rocks!"); 92: 93: private void button1_Click(object sender, 94: System.EventArgs e) 95: { 96: if(mySwitch.Enabled) 97: MessageBox.Show("Boolean switch is enabled!"); 98: else 99: MessageBox.Show("Boolean switch is disabled!"); 100: 101: } 102: } 103: }
Most of the code is Windows Forms designer noise. The form contains a single button with an event handler. It is the event handler and the switch code we are interested in on lines 90 through 101. (The complete listing was provided for reference.)
Lines 90 and 91 declare and initialize the BooleanSwitch. We included the System.Diagnostics namespace on line 7; alternatively, we could have used the complete namespace when declaring and constructing the switch. Once created, the switch mySwitch is used to direct code-flow traffic.
It is worth noting that the name of the switch is not case-sensitive, but you must use the same word. For example, name="mySwitch" is equivalent to name="MySwitch" in the .config file. Additionally, I defined a .config file and added it to the project in the directory in which the executable is located (see Figure 1).
Figure 1 Add the .config file to your project and place it in the executable directory as shown.