/*-------------------------------------------------------------------------------- DO MATH CLASS Modified for C# 2005 -------------------------------------------------------------------------------- Updated 1/29/07 This class takes two numbers and an operand to know what to do on the numbers. X and Y are short integers and Z is a string (+,-,*,/) that tell C# to add/subtract, etc. */ using System; using System.Collections.Generic; using System.Text; namespace UsingClassesPart2 { class DoMath { public double DoCompute(double x, double y ,string z ) { double Result = 0; switch (z) { case "+": Result = x + y; break; case "-": Result = x - y; break; case "*": Result = x * y; break; case "/": Result = x / y; break; } return Result; } } } /*----------------------------------------------------------------------- U S I N G C L A S S E S P A R T 2 Ron Kessler Updated for C# 2005 ----------------------------------------------------------------------- This demo introduces you to some more features of classes and also introduces the "foreach" loop which I use to clear all the textboxes! Updated 1/29/07 */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace UsingClassesPart2 { public partial class frmMain : Form { public frmMain() { InitializeComponent(); } //class-level variables double firstNumber = 0; //the 1st number they enter double secondNumber = 0; //secondNumber number they enter string mathOperand = ""; //what to do? (+ - * /) DoMath getAnswer = new DoMath(); //instantiate my custom class private void optAdd_CheckedChanged(object sender, EventArgs e) { if (optAdd.Checked == true) mathOperand = "+"; } private void optSubtract_CheckedChanged(object sender, EventArgs e) { if (optSubtract.Checked == true) mathOperand = "-"; } private void optMultiply_CheckedChanged(object sender, EventArgs e) { if (optMultiply.Checked == true) mathOperand = "*"; } private void optDivide_CheckedChanged(object sender, EventArgs e) { if (optDivide.Checked == true) mathOperand = "/"; } private void btnCompute_Click(object sender, EventArgs e) { try { firstNumber = Convert.ToDouble(txtNumber1.Text); secondNumber = Convert.ToDouble(txtNumber2.Text); //---use the function DoCompute inside the Class Module called DoMath.cs txtAnswer.Text =Convert.ToString(getAnswer.DoCompute(firstNumber, secondNumber, mathOperand)); } catch (Exception ex) { MessageBox.Show("Sorry, but I can't manage that operation...try again." + ex.Message, "System Message",MessageBoxButtons.OK,MessageBoxIcon.Error); btnClear.PerformClick(); //simulates clicking the clear button! txtNumber1.Focus(); } } private void btnClear_Click(object sender, EventArgs e) { //---this sub finds all the textboxes on the form and clears them! It searches through the controls collection and when // it finds a textbox, it clears it. //---dim a temp object variable as a control object. It can be a button, listbox, textbox, etc. foreach (Control myControl in Controls) //loop through the form's control collection to find all the textboxes. { if (myControl is TextBox) //found textbox? then clear text myControl.Text = ""; } txtNumber1.Focus(); } private void btnEnd_Click(object sender, EventArgs e) { DialogResult button1 = MessageBox.Show("Are you sure", "Sytem Message", MessageBoxButtons.YesNo,MessageBoxIcon.Question); if (button1 == DialogResult.Yes) this.Close(); } } }