Case 3: Size Loss

 

There has always been a need for special methods (earlier called subroutines) that support calculations, repetitive processes, and more in major programs. In this chapter, to demonstrate a subroutine build, we create the simplest subroutine ever known: adding two integers together.

This is the main (and only) window:

image from book
Figure 26-1: Methods window

Project Methods

This project, Methods, calls a subroutine named Subroutine() to add two integers together. The complete project is located at Visual Studio 2005\Projects\DemosSourceCode\Methods.

Three integers (IntegerA, IntegerB, and IntegerC) are declared in line SR013. The subroutine Subroutine() is defined in lines SR016-SR020. This subroutine returns an integer to the calling program, so it must be declared as public int. If a subroutine returns nothing, it is declared as type public void.

The user must first enter an integer in each of the text boxes (there is no type-checking here). In an actual program it would be important to check that each entry is in fact a number and an integer. As soon as the user clicks on the Do the Addition button, the button1_Click event handler swings into action (lines SR030-SR036).

The subroutine is called in line SR034. The answer it returns is placed in IntegerC. The answer is converted to a string and is placed into label5 at line SR035. Label5 appears on the main window between the Do the Addition button and the Quit button.

The source code for Methods is given below.

Methods (Subroutine) (SR) Listing

 Form1.cs: SR002:        using System; SR003:        using System.Collections.Generic; SR004:        using System.ComponentModel; SR005:        using System.Data; SR006:        using System.Drawing; SR007:        using System.Windows.Forms; SR009:        namespace Methods SR010:        { SR010:          partial class Form1 : Form SR012:          { SR013:            public int IntegerA, IntegerB, IntegerC; SR014:            public Form1() SR015:            {InitializeComponent(); }                   // Here is the global 'Method': SR016:            public static int Subroutine(int intOne, int intTwo) SR017:            { SR018:              int Number = intOne + intTwo; SR019:              return(Number); SR020:            } //-----------------------------------------------------------------------------------------// SR030:            private void button1_Click(object sender, EventArgs e) SR031:            { // Do the addition. SR032:              IntegerA = Convert.ToInt32(textBox1.Text); SR033:              IntegerB = Convert.ToInt32(textBox2.Text);                     // Here method 'Subroutine' is used: SR034:              IntegerC = Subroutine(IntegerA, IntegerB); SR035:              label5.Text = "The answer is: " + IntegerC.ToString()+"."; SR036:            } //-----------------------------------------------------------------------------------------// SR040:             private void button2_Click(object sender, EventArgs e) SR041:             { // Quit. SR042:               Close(); SR043:             } SR044:          } SR045:        } //========================================================================================// Form1.Designer.cs: SR100:        namespace Methods SR101:        { SR102:          partial class Form1 SR103:          {                   // Required designer variable. SR104:            private System.ComponentModel.IContainer components = null;                   // Clean up any resources being used. SR105:            protected override void Dispose(bool disposing) SR106:            { SR107:              if (disposing && (components != null)) components.Dispose(); SR108:              base.Dispose(disposing); SR109:            } SR110:            #region Windows Form Designer generated code                   // Required method for Designer support. SR111:            private void InitializeComponent() SR112:            { SR113:              this.label1 = new System.Windows.Forms.Label(); SR114:              this.label2 = new System.Windows.Forms.Label(); SR115:              this.label3 = new System.Windows.Forms.Label(); SR116:              this.label4 = new System.Windows.Forms.Label(); SR117:              this.textBox1 = new System.Windows.Forms.TextBox(); SR118:              this.textBox2 = new System.Windows.Forms.TextBox(); SR119:              this.label5 = new System.Windows.Forms.Label(); SR120:              this.button1 = new System.Windows.Forms.Button(); SR121:              this.button2 = new System.Windows.Forms.Button(); SR122:              this.SuspendLayout();                     //                     // label1                     // SR123:              this.label1.AutoSize = true; SR124:              this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14F,                       System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); SR125:              this.label1.Location = new System.Drawing.Point(145, 16); SR126:              this.label1.Name = "label1"; SR127:              this.label1.Size = new System.Drawing.Size(201, 24); SR128:              this.label1.TabIndex = 0; SR129:              this.label1.Text = "Methods (Subroutines)";                     //                     // label2                     // SR130:              this.label2.AutoSize = true; SR131:              this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); SR132:              this.label2.Location = new System.Drawing.Point(23, 64); SR133:              this.label2.Name = "label2"; SR134:              this.label2.Size = new System.Drawing.Size(431, 17); SR135:              this.label2.TabIndex = 1; SR136:              this.label2.Text = "This program adds two integers together and writes                       the answer below.";                     //                     // label3                     // SR137:              this.label3.AutoSize = true; SR138:              this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); SR139:              this.label3.Location = new System.Drawing.Point(53, 89); SR140:              this.label3.Name = "label3"; SR141:              this.label3.Size = new System.Drawing.Size(127, 17); SR142:              this.label3.TabIndex = 2; SR143:              this.label3.Text = "Enter \'Integer A\': -->";                     //                     // label4                     // SR144:              this.label4.AutoSize = true; SR145:              this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); SR146:              this.label4.Location = new System.Drawing.Point(232, 89); SR147:              this.label4.Name = "label4"; SR148:              this.label4.Size = new System.Drawing.Size(126, 17); SR149:              this.label4.TabIndex = 3; SR150:              this.label4.Text = "Enter \'Integer \'B\' -->";                     //                     // textBox1                     // SR151:              this.textBox1.Location = new System.Drawing.Point(176, 88); SR152:              this.textBox1.Name = "textBox1"; SR153:              this.textBox1.Size = new System.Drawing.Size(40, 20); SR154:              this.textBox1.TabIndex = 4;                     //                     // textBox2                     // SR155:              this.textBox2.Location = new System.Drawing.Point(358, 88); SR156:              this.textBox2.Name = "textBox2"; SR157:              this.textBox2.Size = new System.Drawing.Size(40, 20); SR158:              this.textBox2.TabIndex = 5;                     //                     // label5                     // SR159:              this.label5.AutoSize = true; SR160:              this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F,                       System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,                       ((byte)(0))); SR161:              this.label5.Location = new System.Drawing.Point(160, 168); SR162:              this.label5.Name = "label5"; SR163:              this.label5.Size = new System.Drawing.Size(20, 17); SR164:              this.label5.TabIndex = 6; SR165:              this.label5.Text = "L5";                     //                     // button1                     // SR166:              this.button1.Location = new System.Drawing.Point(187, 130); SR167:              this.button1.Name = "button1"; SR168:              this.button1.Size = new System.Drawing.Size(125, 25); SR169:              this.button1.TabIndex = 7; SR170:              this.button1.Text = "Do the Addition"; SR171:              this.button1.Click += new System.EventHandler(this.button1_Click);                     //                     // button2                     // SR172:              this.button2.Location = new System.Drawing.Point(212, 200); SR173:              this.button2.Name = "button2"; SR174:              this.button2.Size = new System.Drawing.Size(75, 25); SR175:              this.button2.TabIndex = 8; SR176:              this.button2.Text = "Quit"; SR177:              this.button2.Click += new System.EventHandler(this.button2_Click);                     //                     // Form1                     // SR178:              this.AutoScaleDimensions = new System.Drawing.SizeF(5F, 13F); SR179:              this.ClientSize = new System.Drawing.Size(484, 248); SR180:              this.Controls.Add(this.button2); SR181:              this.Controls.Add(this.button1); SR182:              this.Controls.Add(this.label5); SR183:              this.Controls.Add(this.textBox2); SR184:              this.Controls.Add(this.textBox1); SR185:              this.Controls.Add(this.label4); SR186:              this.Controls.Add(this.label3); SR187:              this.Controls.Add(this.label2); SR188:              this.Controls.Add(this.label1); SR189:              this.Name = "Form1"; SR190:              this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; SR191:              this.Text = "Methods (Subroutines)"; SR192:              this.ResumeLayout(false); SR193:              this.PerformLayout(); SR194:            } SR195:            #endregion SR196:            private System.Windows.Forms.Label label1; SR197:            private System.Windows.Forms.Label label2; SR198:            private System.Windows.Forms.Label label3; SR199:            private System.Windows.Forms.Label label4; SR200:            private System.Windows.Forms.TextBox textBox1; SR201:            private System.Windows.Forms.TextBox textBox2; SR202:            private System.Windows.Forms.Label label5; SR203:            private System.Windows.Forms.Button button1; SR204:            private System.Windows.Forms.Button button2; SR205:          } SR206:        } 
 


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