Defining and Using Constants


When you hard-code numbers in your procedures (such as in intVotingAge = 18), a myriad of things can go wrong. Hard-coded numbers are generally referred to as magic numbers because they're often shrouded in mystery; the meaning of such a number is obscure because the digits themselves give no indication of what the number represents. Constants are used to eliminate the problems of magic numbers.

You define a constant as having a specific value at design time, and that value never changes throughout the life of your program. Constants offer the following benefits:

  • Elimination or reduction of data entry problems It's much easier to remember to use a constant named c_pi than it is to enter 3.14159265358979 everywhere that pi is needed. The compiler catches misspelled or undeclared constants, but it doesn't care one bit what you enter as a literal value. (Incidentally, you can retrieve the value of pi using System.Math.PI, so you don't have to worry about creating your own constant!)

  • Code is easier to update If you hard-coded a mortgage interest rate at 6.785, and rate changed to 7.00, you'd have to change every occurrence of 6.785 in code. In addition to the possibility of data entry problems, you'd run the risk of changing a value of 6.785 that had nothing to do with the interest rateperhaps a value that represented a savings bond yield (OK, a very high savings bond yield). With a constant, you change the value once at the constant declaration, and all code that references the constant uses the new value right away.

  • Code is easier to read Magic numbers are often anything but intuitive. Well-named constants, on the other hand, add clarity to code. For example, which of the following statements makes the most sense to you?

    decInterestAmount = ((decLoanAmount * 0.075) * 12);

    or

    decInterestAmount = ((decLoanAmount * c_fltInterestRate) *                     c_intMonthsInTerm);

Constant definitions have the following syntax:

const datatype name = value;


To define a constant to hold the value of pi, for example, you could use a statement such as this:

const double c_pi = 3.14159265358979;


Note how I prefix the constant name with c_. I do this so that it's easier to determine what's a variable and what's a constant when reading code. See the "Naming Conventions" section later in this hour for more information.

After a constant is defined, you can use the constant's name in code in place of the constant's value. For example, to output the result of two times the value of pi, you could use a statement like this (the * character is used for multiplication and is covered in the next hour):

Console.WriteLine(c_pi * 2);


Using the constant is much easier and less prone to error than typing this:

Console.WriteLine(3.14159265358979 * 2);


Constants can be referenced only in the scope in which they are defined. I discuss scope in the section "Determining Scope" later in this hour.

You're going to use what you learn in this chapter to enable the options controls that you added in Hour 7, "Working with Traditional Controls." The first thing you'll do is use constants to create default values for the options. Recall from Hour 7 that you created an option form that allowed the user to manipulate the following three options:

  • The user's name This is displayed in the Picture Viewer's main form title bar.

  • Prompt to confirm on exit This is used to determine whether the user is asked to if they really want to shut down the Picture Viewer application.

  • The default background color of the picture box This can be set to Gray (the default) or white.

In the following steps, you'll create a constant for the default value of the Prompt on Exit option. Start by opening the Picture Viewer project from Hour 10 and then follow these steps:

1.

Click once on frmViewer.cs in the Solutions Explorer to select it.

2.

Click the View Code button at the top of the Solution Explorer to view the code behind frmViewer.vs.

3.

The constants you are about to create will be class-level constants. That is, they can be used anywhere within the module in which they are declared. This means that they will not be placed in a specific procedure. The place to put module constants is right after the declaration of the class (public partial class classname). Position the cursor on the line following the opening brace of the class declaration (see Figure 11.1), press Enter to create a new line, and then enter the following constant declaration:

const bool c_defPromptOnExit = false;


Figure 11.1. Create your class-level constants here.


In the next section, you'll learn how to use this constant to set the value of a variable.




Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net