A variable is a location in the computer's memory where you can temporarily store information, such as a number or a string.
Variables have three components to them:
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 VariablesThe 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.
Declaring a VariableTo 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 VariablesEach 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.
Some examples of valid variable names are
Some examples of invalid variable names are
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 TypesRecall 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.
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 TypesIntegers 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 TypesInteger 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.
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.
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 TypesA 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 TypesA 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 TypesTo 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 TypeVisual 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 StatementAs 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 VariableAs 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. |