Declaring and Using Variables


A variable is a location in the computer's memory where you can temporarily store information, such as a number or a string.

By the Way

In a programming language, a string is a sequence of characters and is delimited by the double quotation marks. An example of a string would be

"Hello, world!" 



Variables have three components to them:

  • A value, such as 5, or "Hello, world!"

  • A name, which is used to refer to the value of the variable

  • A type, which indicates what types of values can be stored. For example, a variable of type Integer could store values like 5 and 858. A variable with type String could store values like "Hello, world!" and "Jisun is a toad".

Did you Know?

Because a variable's type dictates what data can be stored in the variable, a variable's type commonly is referred to as its data type.


Think of a variable as a specific type of box into which you can place things of a certain type. Each box has a name that you give it, which you can use to reference the value in a particular box. For example, as Figure 5.1 shows, a box named age can accept an integer value. Inside this box we can place values like 24, 97, 3,829,294, or any other valid integer value.

Figure 5.1. Think of a variable as a named box that can contain a certain type of value.


Assigning Values to Variables

The name and data type of a variable are immutable. That is, once the variable's name and type have been specified, they cannot change during the program's execution. The variable's value, on the other hand, is mutable, meaning that it can change over the course of the program's execution.

Variables alter their value through assignment statements. An assignment statement assigns a value to a variable using the = operator and has the following form:

variableName = value 


This statement assigns value to the value of the variable variableName.

By the Way

We will discuss the = operator (often referred to as the assignment operator) in the "Visual Basic's Assignment Operators" section.


Declaring a Variable

To use a variable, you must first declare the variable using the Visual Basic Dim statement. When declaring a variable, you must provide both the name and data type of the variable and optionally may include the value. For example, to create a variable named age that accepts values of type Integer, you would use the following Dim statement:

Dim age as Integer 


More generally, the Dim statement has the following form:

Dim variableName as type 


We'll examine the Dim statement in much greater detail in the "Examining the Dim Statement" section. First, though, we need to look at the rules for naming variables, as well as the available variable types.

Rules for Naming Variables

Each programming language imposes its own set of rules for naming variables. For Visual Basic, variable names can start with an alphabetic character or an underscore character, followed by zero to many underscores, alphabetic characters, or numeric characters.

By the Way

Variable names in Visual Basic may be anywhere from one character long to 16,383 characters long. They are not case sensitive, meaning that the casing does not matter. Therefore, the variable name Age is equivalent to the variable names AGE, age, aGe, and so on.


Some examples of valid variable names are

  • Age

  • message2

  • xyz123abc

  • txtPassword

Watch Out!

If a variable name begins with an underscore, it must be followed by at least one other character. That is, you cannot have a variable simply named _.


Some examples of invalid variable names are

  • 3Age Invalid because a variable name cannot start with a numeric character.

  • _ Invalid because if a variable name begins with an underscore, it must be followed by at least one other character.

  • 234 Invalid because a variable name cannot start with a numeric character.

When you're naming your variables, it is important to choose names that make sense given the information the variable will store. For example, if you are going to use a variable to store the product of two numbers, you might want to name that variable product, or some other descriptive name, rather than using something ambiguous, like x or variable3.

Examining Variable Data Types

Recall that the data type of a variable dictates what type of value can be stored in the variable. For example, a variable of type Integer can store only values that are integers (negative and positive whole numbers).

If you have worked with ASP, ASP.NET's predecessor, you've likely had experience with VBScript, a watered-down version of Visual Basic 6.0, which was the version of Visual Basic that predated Microsoft's .NET platform. With VBScript, variables were loosely typed.

Loosely typed variables are variables that are declared without an explicit data type. Their type is inferred by the value assigned to the variable. For example, in VBScript you could write code that looked like so:

1: Dim x 2: x = "Hello, World!" 3: x = 4 


Note that the Dim statement on line 1 does not contain a type (that is, it does not read: Dim x as String). The type of x is dynamically inferred by the value assigned to it. Therefore, on line 2, when x is assigned the value "Hello, World!", which is a string, x's type is considered to be of type String. On line 3, however, x is assigned the value 4, which is an integer. After line 3, x's type now is considered to be of type Integer.

The opposite of loosely typed is strongly typed. In strongly typed languages, all variables must be declared to be of a certain type. After a variable has been declared to be a certain type, it can be assigned only values that correspond to that type.

By the Way

With loosely typed languages, any value can be assigned to any variable, and the value being assigned determines the variable's type. With strongly typed languages, a variable's type is explicitly specified, and only values corresponding to the variable's type may be assigned to the variable.


Visual Basic is a strongly typed language. Therefore, all variables must be given an explicit data type, and the set of values that can be assigned to a given variable is limited by the variable's type.

Because each type has a predefined set of values that can be assigned, it is important to give your variable the proper type. For example, in Hour 4, "Designing, Creating, and Testing ASP.NET Web Pages," we looked at an ASP.NET web page that calculated the monthly cost of a home loan. The variables used to hold the intermediary computations were of type Double, which is a numeric type that stores numbers with decimal places. Had we chosen to use integer variables, the calculation would have come out incorrectly because we were dealing with decimal numbers.

Specifically, the interest rate involved in the calculation, which might be 0.065 (for a 6.5% interest rate), could not be expressed as an integer. Rather, we would have to use 0 or 1 (or some other whole number), which would clearly produce an incorrect answer. For this reason, using the correct type is important.

In general, when declaring a variable, you use the following syntax:

Dim variableName as type 


Integer Types

Integers are whole numbers that can be either positive or negative. For example, 34, 76, 3,432, and 234,124 are all valid integers, whereas 12.4 and 3.14159 are not.

There are three types of integer data types, each differing in the range of integers it can contain. The most common integer type is type Integer, which can accept values ranging from 2,147,483,648 to 2,147,483,647. To create a variable of type Integer, use the following syntax:

Dim variableName as Integer 


If you need to store larger or smaller integer values, you can use the Long data type, which accepts integer values ranging from 9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. To create a variable of type Long, use the following syntax:

Dim variableName as Long 


If you need to store much smaller integer values, you can use the Short data type, which can store integers ranging from 32,768 to 32,767. To create a variable of type Short, use

Dim variableName as Short 


Nonintegral Numeric Types

Integer variables cannot store numbers that have decimals. However, very often calculations involving numbers with decimals need to be performed. To provide for this, Visual Basic offers three nonintegral numeric data types.

By the Way

A nonintegral numeric data type is a data type that has a specific number of significant digits but can have the decimal place moved to improve the accuracy of the decimal portion.


The first nonintegral numeric data type is Single, which can accept values ranging from 3.4028235E+38 through 1.401298E45 for negative values and from 1.401298E45 through 3.4028235E+38 for positive values.

By the Way

In scientific notation, the number following the E represents the number's magnitude. The magnitude specifies the number of decimal places in the number. For example, 6.45E+8 is equal to 6.45 * 10^8, or 645,000,000, and 6.45E-8 is equal to 6.45 * 10^-8, or 0.0000000645. Therefore, 3.4028235E+38 is a very big number!


To create a variable of type Single, use the following syntax:

Dim variableName as Single 


A more precise nonintegral numeric data type that also allows for larger numbers is Double, which can accept values ranging from 1.79769313486231570E+308 through 4.94065645841246544E324 for negative values and from 4.94065645841246544E324 through 1.79769313486231570E+308 for positive values. To create a variable of type Double, use the following syntax:

Dim variableName as Double 


The third and final nonintegral numeric data type is Decimal, which scales the decimal place by powers of 10. Decimals can have from 0 to 28 decimal places. With zero decimal places, the largest number a Decimal can have is 79,228,162,514,264,337,593,543,950,335 (the smallest being 79,228,162,514,264,337,593,543,950,335). The Decimal can have, at most, 28 decimal digits. Hence, the largest number with 28 decimal digits is 7.9228162514264337593543950335. To create a variable of type Decimal, use the following syntax:

Dim variableName as Decimal 


Boolean Data Types

A Boolean variable is a variable that can be assigned only one of two values: either true or False. To create a Boolean variable, use the Boolean variable type. The following syntax demonstrates how to create a variable of type Boolean:

Dim variableName as Boolean 


String Types

A string is a sequence of characters. For example, "ASP.NET is fun!" is a string composed of 15 characters, the first being A, the second being S, and so on, with the 15th one being !. To create a variable that can store string values, use the type String. To create a variable of type String, use the following syntax:

Dim variableName as String 


Date Types

To allow for a variable to store specific dates, specify the variable's data type as Date using the following syntax:

Dim variableName as Date 


A Date variable can store dates between midnight on January 1, 0001, through 11:59:59 p.m. on December 31, 9999.

The Object Type

Visual Basic contains a catchall type, a data type that can be assigned any value. This base type is the Object type. The Object type is, by its nature, extremely flexible because you can assign a variable of any type to it. For example, as the following code shows, you can assign a string to an Object type, and then an integer, and then a nonintegral number:

Dim catchall as Object catchall = "Jisun" catchall = 4 catchall = 3.14159 


Despite its flexibility, you should rarely, if ever, create a variable of type Object. The benefit of using more specific types like Integer, String, and Double is that if you accidentally try to assign an inappropriate value to one of these variables, an error message will be displayed.

Examining the Dim Statement

As we discussed earlier, before using a variable, you must declare the variable. When declaring a variable in a strongly typed programming language, you must specify not only the name of the variable, but also the variable's type. In Visual Basic, this is accomplished using the Dim statement.

In its simplest form, the Dim statement simply specifies the variable's name and type, as follows:

Dim variableName as type 


If you want to declare three variables of type Integer, you can use three separate Dim statements, such as

Dim a as Integer Dim b as Integer Dim c as Integer 


Or you can use one Dim statement, separating each variable name and type with a comma, such as

Dim a as Integer, b as Integer, c as Integer 


You can also supply a comma-delimited list of variable names and just one type. In this instance, all of the variable names appearing before the one type will have the same type. That is, you can declare three variables, a, b, and c, all to be of type Integer using the following syntax:

Dim a, b, c as Integer 


Performing Assignment When Declaring a Variable

As we've seen thus far, the Dim statement is used for declaring a variable but not for assigning the variable a value. That is, if you wanted to create a variable named a of type Integer and have it assigned the value 6, you'd use the following code:

Dim a as Integer a = 6 


The preceding syntax is fine as-is, but you can save yourself a line of code by combining the assignment with the variable declaration on one line. To do this, use the following syntax:

Dim a as Integer = 6 


Or, more generally

Dim variableName as type = value 


Performing an assignment when declaring a variable is purely optional. We will use this variant of the Dim statement in many examples in this book in the interest of saving space.




Sams Teach Yourself ASP. NET 2.0 in 24 Hours, Complete Starter Kit
Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit
ISBN: 0672327384
EAN: 2147483647
Year: 2004
Pages: 233

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