Defining and Using Constants

   

When you hard-code numbers in your code (such as in intVotingAge = 19;), 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 as to 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 is much easier, for example, to remember to use a constant named c_pi than it is to enter 3.14159265358979 everywhere that pi is needed. The compiler will catch 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 rates were changed to 7.00, you would 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 rate ”perhaps a value that represented a savings bond yield. With a constant, you change the value once, and all code uses the new value.

  • 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?

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

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

Constant definitions have the following syntax:

 const datatype  name  =  value;  

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

 const float 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 section on naming conventions later in this hour for more information.

After a constant is defined, you can use the constant's name anywhere 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):

 Debug.WriteLine(c_pi * 2); 

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

 Debug.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."


   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

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