/*_________________________________________________________________________ U S I N G S W I T C H S T A T E M E N T S Created 12/27/06 for C# 2005 Ron Kessler ___________________________________________________________________________ Updated 12/27/06 This program demonstrates how to use the switch structure. They choose a State & we compute the sales tax rate. Notice the options I show you for declaring data types for the tax rate. Just remember the compiler defaults to a double data type so you have to use "m" or "M" if you want decimal. */ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Using_Switch { public partial class frmMain : Form { public frmMain() { InitializeComponent(); } //______________________________________________________________________________________________________ private void btnOK_Click(object sender, EventArgs e) { //---now decide the sales tax rate based upon the state they chose double salesTaxRate = 0; //you could say decimal salesTaxRate = 0 //But then you must say salesTaxRate = .08m when you use it. Think of m as money. //The compiler defaults to double precision unless you tell it otherwise. Seems redundant doesn't it! string stateChosen = txtState.Text.ToUpper(); //control for caps/lower case switch (stateChosen) { case "CA": salesTaxRate = .08; //salesTaxRate = .08m; if using a decimal type. Must use "f" or "F" for a float (single data type) break; case "AZ": salesTaxRate = .05; break; case "NV": salesTaxRate = .01; break; case "OR": salesTaxRate = .06; break; default: MessageBox.Show( "Please enter CA, AZ, OR, NV", "Ron's Place", MessageBoxButtons.OK, MessageBoxIcon.Information); break; } if (salesTaxRate > 0) //did they enter a valid state? { MessageBox.Show("Your Tax Rate for " + stateChosen + " is " + salesTaxRate, "System Message"); } txtState.Clear(); //start over txtState.Focus(); } //____________________________________________________________________________________ private void btnQuit_Click(object sender, EventArgs e) { DialogResult button = MessageBox.Show( "Are you sure?", "System Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (button == DialogResult.Yes) this.Close(); } } }