May 5, 2002 - Defining Constant Variables
May 5, 2002 Defining Constant Variables Tips: May 2002
Yehuda Shiran, Ph.D.
|
const
declaration is very common in other programming languages. It allows you to declare a variable as non-changing. The values of variables declared as const
remain constant throughout the life of the application. The advantage of using the const
declaration is in making the variable's maintenance a lot easier. When you debug a program, it's very helpful to know which variables can change and which variables cannot change. The syntax of the const
declaration is as follows:
const name1 [: type1] = value1 [, ... [, nameN [: typeN] = valueN]];
where:
name1, ..., nameN
are the names of the constants being declared. They are, of course, required. Also:
type1, ..., typeN
are the types of the constants being declared. As mentioned in Page 2, the data types are optional in JScript .NET. Finally:
value1, ..., valueN
are the values assigned to the constants. Here are some examples:
const index = 5;
const president : String = "Abraham Lincoln";
const factor : int = 35, twoThirds : float = 2./3.;
To learn more on JScript .NET, go to Column 108, JScript .NET, Part II: Major Features.