13.07.2015 Views

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

ASP.NET 3.5: A Beginner's Guide - www.mustafaof.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3: C# and <strong>ASP</strong>.<strong>NET</strong> <strong>3.5</strong> 55A constant is like a variable in that it stores values, but unlike a variable, a constantrepresents an unchanging value. For example, the value of the mathematical constant pi isunchanging, and by placing its value into a constant, you’re assured that some other valuewill not creep in. Actually, any value can be placed into a constant whether it’s absolutelike the value of pi or the freezing point of water or some other value that you want foryour program. Let’s say you have a program to check for the legal voting age of 18 thatcalculates age based on current date and birthday. The voting age needs to be constant,while you expect the current date and birthday to change. Then, suppose that the votingage changes from 18 to 21 because your calculation is based on a different country. Theonly value you would need to change is the constant that holds the voting age.Using constants and variables of the same type together works well. For example, thefollowing statements will calculate values fine:const int VAGE = 18;int yourAge = 35;int legalAge;legalAge = yourAge - VAGE; //Expression using both constant and variableThe important aspect of the script is not whether an element is a variable or constant, butwhether the types of the variables and constants are the same. In this case, both variables andthe constant are of the same type, int; therefore, they work together. The following scriptpair illustrates how C# and <strong>ASP</strong>.<strong>NET</strong> use constants and variables in concert.<strong>ASP</strong>.<strong>NET</strong> ConVarConstants and Variables

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!