264
LESSON 22 Preventing Bugs
// Calculate the average.
decimal averageSalary = AverageSalary(salaries);
// Display the result.
averageTextBox.Text = averageSalary.ToString(“C”);
}
catch (Exception ex)
{
averageTextBox.Clear();
MessageBox.Show(ex.Message);
}
}
Again a real program shouldnt let the user enter salaries as string like this because
the user could enter invalid values.
Make the
AverageSalary method validate its inputs by asserting that the array has a reason-
able number of elements and that the salaries are reasonable. (Assume you’re not working on
Wall Street so salaries are at least $10,000 and less than $1 million.) Also validate the average.
1. You can use code similar to the following:
// Calculate the average of this array of salaries.
private decimal AverageSalary(decimal[] salaries)
{
// Sanity checks.
if (salaries.Length < 1)
{
throw new ArgumentOutOfRangeException(“salaries”,
“AverageSalary method cannot calculate average “ +
“salary for an empty array.”);
}
Debug.Assert(salaries.Length < 100, “Too many salaries.”);
for (int i = 0; i < salaries.Length; i++)
{
Debug.Assert(salaries[i] >= 10000, “Salary is too small.”);
Debug.Assert(salaries[i] < 1000000, “Salary is too big.”);
}
// Calculate the result.
decimal total = 0;
for (int i = 0; i < salaries.Length; i++)
{
total += salaries[i];
}
decimal result = total / salaries.Length;
// Validate the result.
Debug.Assert(result >= 10000, “Average salary is too small.”);
Debug.Assert(result < 1000000, “Average salary is too big.”);
return result;
}
596906c22.indd 264 4/7/10 12:33:39 PM
Exercises
265
Please select Lesson 22 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. Suppose you’re writing a routine for sorting orders based on priority. Use the following defi-
nition for an
Order structure:
private struct Order
{
public int OrderId;
public int Priority;
}
Write the SortOrders method, which takes as a parameter an array of Orders and sorts
them. Don’t actually write the code that sorts the orders, just write assertions to validate the
inputs and outputs.
2. Build the program shown in Figure 22-3 to convert temperatures between the Fahrenheit,
Celsius, and Kelvin scales. Write methods
FahrenheitToCelsius, KelvinToCelsius,
CelsiusToFahrenheit, and CelsiusToKelvin to perform the conversions using the fol-
lowing formulas:
C = (F – 32) * 5 / 9
C = K – 273.15
F = C
*
9 / 5 + 32
K = C + 273.15
Use assertions to help the conversion methods ensure that
Fahrenheit values are between –130 and 140, Celsius values
are between –90 and 60, and Kelvin values are between 183
and 333.
3. Make a program that lets the user input miles and gallons of fuel and calculates miles per gal-
lon using a
MilesPerGallon method. Make the method protect itself against miles and gallons
values that are too big or too small. Make it also
validate its result so it doesn’t return values that are too large or small.
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 those
solutions in the Lesson22 folder.
FIGURE 223
596906c22.indd 265 4/7/10 12:33:39 PM
Click here to Play
596906c22.indd 266 4/7/10 12:33:39 PM
..................Content has been hidden....................

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