The Six Using Statements

 

A Common For Statement Error

A common for error is to assume globality that doesn t exist. Consider the following for statement, which may be exited after 300 cycles or when the if statement inside is satisfied:

 'int ItemNumber' receives a value here. for (int jj = 0; jj < 300; jj++) {   if(ItemNumber == StorageStack[jj]) goto JUMP; } JUMP: // Ready to proceed. 

When the sequence arrives at JUMP: we tend to assume that we now know which StorageStack() contains the ItemNumber set at the top of the code. So we finish the action with statements like:

 if(jj < 300) { Do something..... } else { Do something different..... } 

We know that the iteration variable in the for statement, jj, is incremented at the bottom of the loop, and when it reaches the value 300 the loop will fail. Hence, we decide to test jj after the JUMP: statement to see if we found what were looking for.

The Visual Studio C# compiler will nail you in a microsecond! It will tell you that the jj found in the statement if(jj < 300) cannot be identified.

What is wrong with this code? The int jj that appears in the first line of the for statement is a local variable that services only this for loop. As soon as the for loop is exited, variable jj ceases to exist. You must introduce a separate, global integer variable that is declared earlier ” a variable that remembers the value of jj when the for loop is exited:

 for(int jj = 0; jj < 300; jj++) {   if(ItemNumber == StorageStack[jj])   {     RememberNumber = jj;     goto JUMP;   } } JUMP: // Ready to proceed. if(RememberNumber < 300) { Do something..... } else { Do something else..... } 

All programmers do strange things like this from time to time.

CodePlacement (CP)

 Form1.cs: CP001:          using System; CP002:          using System.Collections.Generic; CP003:          using System.ComponentModel; CP004:          using System.Data; CP005:          using System.Drawing; CP006:          using System.Windows.Forms; /*CP009*/     namespace CodePlacement               { /*CP010*/       partial class Form1 : Form                 { /*CP011*/         public int intNumber; // = 6; /*CP012*/         // int intGlobalNumber = 77; /*CP013*/         public int[] intArray = new int[] {1, 22, 88}; /*CP014*/         public Form1()                   {                     InitializeComponent(); /*CP016*/ /*CP017*/                                               } /*CP099*/         // int intGlobalNumber = 77; //-----------------------------------------------------------------------------------------// /*CP101*/         private void button1_Click(object sender, EventArgs e) /*CP102*/         { // Start Demo.  /*CP103*/           intNumber = 92; /*CP104*/           // MessageBox.Show("intGlobalNumber = "                     // +intGlobalNumber.ToString() + " ."); /*CP105*/         } //-----------------------------------------------------------------------------------------// /*CP201*/         private void button2_Click(object sender, EventArgs e) /*CP202*/         { // Call Child Window. Open Form2. /*CP203*/           // 'this' below sends Form1 'handle' to Form2. /*CP204*/           Form2 frm2 = new Form2(this ); /*CP205*/           frm2.ShowDialog(); /*CP206*/           frm2.Close();                   } //-----------------------------------------------------------------------------------------//                   private void button3_Click(object sender, EventArgs e)                   { // Quit.                     Close();                   }                 }               } //=========================================================================================// Form1.Designer.cs CP300:        namespace CodePlacement               {                 partial class Form1                 {                   // Required designer variable.                   private System.ComponentModel.IContainer components = null;                   // Clean up any resources being used.                   protected override void Dispose(bool disposing)                   {                     if (disposing && (components != null)) components.Dispose();                     base.Dispose(disposing);                   }                   #region Windows Form Designer generated code                   // Required method for Designer support.                   private void InitializeComponent()                   {                     this.label1 = new System.Windows.Forms.Label();                     this.button1 = new System.Windows.Forms.Button();                     this.button2 = new System.Windows.Forms.Button();                     this.button3 = new System.Windows.Forms.Button();                     this.SuspendLayout();                     //                     // label1                     //                     this.label1.AutoSize = true;                     this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F,                     ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold |                     System.Drawing.FontStyle.Underline))), System.Drawing.GraphicsUnit.Point,                     ((byte)(0)));                     this.label1.Location = new System.Drawing.Point(57, 16);                     this.label1.Name = "label1";                     this.label1.Size = new System.Drawing.Size(264, 21);                     this.label1.TabIndex = 0;                     this.label1.Text = "Visual Studio C# Code Placement";                     //                     // button1                     //                     this.button1.Location = new System.Drawing.Point(58, 70);                     this.button1.Name = "button1";                     this.button1.Size = new System.Drawing.Size(100, 23);                     this.button1.TabIndex = 1;                     this.button1.Text = "Start Demo";                     this.button1.Click += new System.EventHandler(this.button1_Click);                     //                     // button2                     //                     this.button2.Location = new System.Drawing.Point(204, 70);                     this.button2.Name = "button2";                     this.button2.Size = new System.Drawing.Size(125, 23);                     this.button2.TabIndex = 2;                     this.button2.Text = "Call Child Window";                     this.button2.Click += new System.EventHandler(this.button2_Click);                     //                     // button3                     //                     this.button3.Location = new System.Drawing.Point(153, 114);                     this.button3.Name = "button3";                     this.button3.Size = new System.Drawing.Size(76, 23);                     this.button3.TabIndex = 3;                     this.button3.Text = "Quit";                     this.button3.Click += new System.EventHandler(this.button3_Click);                     //                     // Form1                     //                     this.AutoScaleDimensions = new System.Drawing.SizeF(5F, 13F);                     this.ClientSize = new System.Drawing.Size(374, 158);                     this.Controls.Add(this.button3);                     this.Controls.Add(this.button2);                     this.Controls.Add(this.button1);                     this.Controls.Add(this.label1);                     this.Name = "Form1";                     this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;                     this.Text = "Code Placement";                     this.ResumeLayout(false);                     this.PerformLayout();                   }                   #endregion                   private System.Windows.Forms.Label label1;                   private System.Windows.Forms.Button button1;                   private System.Windows.Forms.Button button2;                   private System.Windows.Forms.Button button3;                 }               } //=========================================================================================// Form2.cs               [7 lines]               namespace CodePlacement               { /*CP301*/       partial class Form2 : Form                 {                   public Form1 frmParent; /*CP401*/         public Form2(Form1 frm)                   { /*CP402*/           frmParent = frm; /*CP403*/           InitializeComponent(); /*CP404*/ /*CP405*/ /*CP406*/         } //-----------------------------------------------------------------------------------------//                   private void button1_Click(object sender, EventArgs e)                   { // Return to main.                     Close();                   } //-----------------------------------------------------------------------------------------// /*CP601*/         private void Form2_Load(object sender, EventArgs e)                   { /*CP602*/           int intAnswer = frmParent.intNumber; /*CP603*/           MessageBox.Show("intNumber from Form1 = " +                     intAnswer.ToString() + " .");                   }                 }                } //=========================================================================================// Form2.Designer.cs               namespace CodePlacement               {                 partial class Form2                 {                   // Required designer variable.                   private System.ComponentModel.IContainer components = null;                   // Clean up any resources being used.                   protected override void Dispose(bool disposing)                   {                     if (disposing && (components != null)) components.Dispose();                     base.Dispose(disposing);                   }                   #region Windows Form Designer generated code                   // Required method for Designer support.                   private void InitializeComponent()                   {                     this.button1 = new System.Windows.Forms.Button();                     this.SuspendLayout();                     //                     // button1                     //                     this.button1.Location = new System.Drawing.Point(106, 42);                     this.button1.Name = "button1";                     this.button1.TabIndex = 0;                     this.button1.Text = "Return";                     this.button1.Click += new System.EventHandler(this.button1_Click);                     //                     // Form2                     //                     this.AutoScaleDimensions = new System.Drawing.SizeF(5F, 13F);                     this.ClientSize = new System.Drawing.Size(292, 107);                     this.Controls.Add(this.button1);                     this.Name = "Form2";                     this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;                     this.Text = "Child Window";                     this.Load += new System.EventHandler(this.Form2_Load);                     this.ResumeLayout(false);                   }                   #endregion                   private System.Windows.Forms.Button button1;                 }               } 

If you want to redo the CodePlacement project (and are concerned that the code in this project has been changed by your actions), then find the Form1Spare.cs file in this project and make a copy of it, change the copy s name to Form1.cs, and overwrite the old Form1.cs that you have altered .

 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net