224
LESSON 18 Making ChoiCes
statements2;
break;
...
default:
statementsDefault;
break;
}
Here testValue is the value that you are testing; value1, value2, and so on, are the values to
which you are comparing
testValue; and statements1, statements2, and so on, are statements
that you want to execute for each case. The other pieces (
switch, case, break, and default) are
keywords that you must type as they appear here.
If you include the optional
default section, its statements execute if no other case applies. (Actually
you dont need to include any
case statements, either, although that would be unusual.)
Note that a
cases code block doesn’t need to include any statements other than break. You can use
that to make the code take no action when a particular
case occurs.
For example, suppose you build a form where the user selects a hotel from a combobox. The pro-
gram uses that selection to initialize an enumerated variable named
hotelChoice. The following
code sets the
lodgingPrice variable depending on which hotel the user selected:
decimal lodgingPrice;
switch (hotelChoice)
{
case HotelChoice.LuxuryLodge:
lodgingPrice = 45;
break;
case HotelChoice.HamiltonArms:
lodgingPrice = 80;
break;
case HotelChoice.InvernessInn:
lodgingPrice = 165;
break;
default:
MessageBox.Show(“Please select a hotel”);
lodgingPrice = 0;
break;
}
The case statements check for the three expected choices and set lodgingPrice to the appropriate
value. If the user doesn’t select any hotel, the
default section’s code displays a message box and sets
lodgingPrice to 0 to indicate a problem.
A
switch statement is most robust (less prone to bugs and crashes) if its cases can handle every
possible value. That makes them work very well with enumerated types because you can list
every possible value.
596906c18.indd 224 4/8/10 8:13:18 AM
Try It
225
Even then, it’s good practice to include a default section just in case another value sneaks into the
code. For example, a bug in the code could convert an integer into an enumeration value that doesn’t
exist, or you could later add a new value to the enumeration and forget to add a corresponding
case statement. In those cases, the default statement can catch the bug or change, take some default
action, and possibly warn you that something is wrong.
When you use other data types for the switch’s value, be
sure to consider unexpected values, particularly if the user
entered the value. For example, dont assume the user will
always enter a valid string. Allowing the user to select a
string from a combobox is safer, but you should still include
a
default statement.
TRY IT
In this Try It, you build the OrderForm program shown
in Figure 18-1. The program uses a cascading series of
if
statements to calculate shipping cost based on the subtotal.
You can download the code and resources for this Try It from the book’s web
page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find
them in the Lesson18 folder in the download.
Lesson Requirements
Build the form shown in Figure 18-1.
Write the code for the Calculate button so it calculates the subtotal, sales tax, shipping, and
grand total. The sales tax should be 7 percent of the subtotal and shipping should be as follows:
$5 if subtotal < $20, $7.50 if subtotal < $50, $10 if subtotal < $75, and free if subtotal $75.
Hints
Make the sales tax rate a constant, giving it the most limited scope you can.
Step-by-Step
Build the form shown in Figure 18-1.
1. This is relatively straightforward.
Write the code for the Calculate button so it calculates the subtotal, sales tax, shipping, and
grand total. The sales tax should be 7 percent of the subtotal and shipping should be as follows:
$5 if subtotal < $20, $7.50 if subtotal < $50, $10 if subtotal < $75, and free if subtotal $75.
FIGURE 181
596906c18.indd 225 4/8/10 8:13:18 AM
226
LESSON 18 Making ChoiCes
1. Calculate the total costs for each of the four items. Add them together to get the subtotal.
2. Calculate sales tax by multiplying the tax rate by the subtotal.
3. Use a series of cascading if-else statements to calculate the shipping cost based on
the subtotal as in the following code:
// Calculate shipping cost.
decimal shipping;
if (subtotal < 20)
{
shipping = 5;
}
else if (subtotal < 50)
{
shipping = 7.5m;
}
else if (subtotal < 75)
{
shipping = 10;
}
else
{
shipping = 0;
}
4. Add the subtotal, tax, and shipping cost to get the grand total.
Please select Lesson 18 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. Build the ConferenceCoster program shown in Figure 18-2.
FIGURE 182
When the user clicks the Calculate button, first check each ListBox’s SelectedIndex
property. If any
SelectedIndex is less than zero (indicating the user didn’t make a choice),
display an error message.
596906c18.indd 226 4/8/10 8:13:19 AM
Exercises
227
If the user made a choice for all of the ListBoxes, create a variable total to hold the total
cost. Use three
switch statements to add the appropriate amounts to total and display the
result. (Tip: Add a
default statement to each switch statement to catch unexpected selec-
tions, even though none should occur in this program. Try adding a new hotel and see what
happens if you select it.)
2. (SimpleEdit) Copy the SimpleEdit program you built in Lesson 8, Exercise 3 (or download
Lesson 8’s version from the book’s web site) and add code to protect the user from losing
unsaved changes.
The basic idea is to check whether the document has been modified before doing anything
that will lose the changes, such as starting a new document, opening another file, or exiting
the program.
a. In the File menu’s New, Open, and Exit event handlers, check the RichTextBox’s
Modified property to see if the document has unsaved changes.
b. If there are unsaved changes, ask if the user wants to save them. Display a message box
with the buttons Yes, No, and Cancel.
c. If the user clicks Yes, save the changes and continue the operation.
d. If the user clicks No, don’t save the changes (do nothing special) and let the operation
continue.
e. If the user clicks Cancel, don’t perform the operation. For example, don’t open a new
file.
f. After starting a new document or saving an old one, set the RichTextBox control’s
Modified property to false to indicate that there are no unsaved changes any more.
3. (SimpleEdit) Copy the SimpleEdit program you built for Exercise 2. That program protects
against lost changes if the user opens the File menu and selects Exit, but there are several
other ways the user can close the program such as pressing [Alt]+F4, clicking the “X” button
in the program’s title bar, and opening the system menu in the form’s upper left corner and
selecting Close. Currently the program doesn’t guard unsaved changes for any of those.
To fix this, give the form a
FormClosing event handler. When the form is about to close,
it raises this event. If you set the event’s
e.Cancel parameter to true, the form cancels the
close and remains open. Add code to this event handler to protect unsaved changes.
Now that the
FormClosing event handler is protecting against lost changes, you dont need
to perform the same checks in the Exit menu item’s event handler. Make that event handler
simply call
this.Close and FormClosing will do the rest.
You can download the solutions to these exercises from the book’s web page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find them in
the Lesson18 folder.
596906c18.indd 227 4/8/10 8:13:19 AM
Click here to Play
596906c18.indd 228 4/8/10 8:13:19 AM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.210.219