Fundamentals of Variables Introduction to Variables A variable is a value or data that must be stored in the computer memory and accessed when necessary. Before using or accessing that variable, you must declare it. This is done by providing at least a name for the variable and an initial value to store in the computer memory. The Name of a Variable A variable must have a name. There are keywords you must not use because the language itself uses them. These keywords are:
The Type of a Variable The amount of computer memory necessary to store the value of a variable is called a data type. Declaring a Variable To declare a variable, type the Dim keyword followed by a name. Here is an example: <%
Dim something
%>
You must then specify how much memory will be necessary to store the value of the variable. You have many options. You can assign a value to the variable. As an alternative, you can follow the variable name with the As keyword followed by the data type. Initializing a Variable Initializing a variable consists of storing an initial value in its reserved memory area. To assign a value to a variable:
The Data Type of a Variable To specify the amount of memory that a variable would use, on the right side of the variable's name, type the As keyword followed by the data type. The formula to declare such a variable is: Dim variable-name As data-type Type Characters To make variable declaration a little faster and even convenient, you can replace the As DataType expression with a special character that represent the intended data type. Such a character is called a type character. It depends on the data type you intend to apply to a variable. When used, the type character must be the last character of the name of the variable. We will see what characters are available and when they can be applied. Introduction to Characters and Strings Characters A character is anything that can be displayed as a symbol. To declare a variable that would hold a character, use a keyword named Char. To initialize the variable, assign a single character included in double-quotes. Here is an example: <%
Dim letter As Char
Letter = "W"
%>
To indicate that the value of the variable must be treated as Char, when initializing it, you can type c or C on the right side of the double-quoted value. Here is an example: <%
Dim letter As Char
Letter = "W"c
%>
To convert a value to Char, use CChar(). To do this, write the value in the parentheses. Introduction to Strings A string is an empty space, a character, a word, or a group of words. To declare a variable for a string, use the String data type. You can initialize the variable with an empty space, a character, a symbol, a word, or a group of words. Here are two examples: <% Dim firstName As String = "Philippe" Dim lastName As String = "Fields" %> The type character for the String data type is $. This means that you can replace the AS String expression with the $ symbol when declaring a string variable. This can be done as follows: <%
Dim sentence$
%>
After declaring a String variable, to initialize it, include its value between double quotes. If you want to include a double-quote character in the string, you can double it. Here is an example: <% Dim sentence$ sentence$ = "Then she said, ""I don't love you no more."" and I left" %> Converting a Value to String To convert a value to a string, use CStr(). In the parentheses, enter the value to convert. Primary Topics on Variables Responding to the Value of a Variable To display the value of a string variable, you can simply put it in the parentheses of Response.Write(). Here are examples: <%@ Page Language="VB" %> <!DOCTYPE html> <html> <head> <title>Employee Record</title> </head> <body> <h2>Employee Record</h2> <% Dim firstName As String = "Philippe" Dim lastName As String = "Fields" Response.Write("First Name: ") Response.Write(firstName) Response.Write("<br>") Response.Write("Last Name: ") Response.Write(lastName) %> </body> </html> This would produce: Value Updating After declaring a variable, you can change its value whenever you judge it necessary. After declaring a variable, you can assign it any value of you choice. Here are examples: <%@ Page Language="VB" %> <!DOCTYPE html> <html> <head runat="server"> <title>Exercise</title> </head> <body> <h2>Exercise</h2> <% Dim something something = 25 Response.Write(something) something = "<p>Welcome to our site.</p>" Response.Write(something) something = 237565.408 Response.Write(something) %> </body> </html> This would produce: Declaring Many Variables Because a section of code can use different variables, you can declare each variable on its own line. Different variables can be declared with the same data type. Here is an example: <%
Dim firstName As String
Dim lastName As String
%>
When various variables use the same data type, instead of declaring each on its own line, you can declare two or more of these variables on the same line. There are two techniques you can use:
After declaring the variables, you can initialize and use them normally. Assigning a Value to a Variable After declaring and initializing a variable, if you want to change its value at any time, type its name, followed by =, and followed by the desired value. This is referred to as assigning a value to the variable. Here are examples: <%@ Page Language="VB" %>
<!DOCTYPE html>
<html>
<head>
<title>Employee Record</title>
</head>
<body>
<h2>Employee Record</h2>
<%
Dim firstName As String = "Philippe"
Dim lastName As String = "Fields"
Response.Write("First Name: ")
Response.Write(firstName)
Response.Write("<br>")
Response.Write("Last Name: ")
Response.Write(lastName)
firstName = "David"
lastName = "Absalom"
Response.Write("<br><br>First Name: ")
Response.Write(firstName)
Response.Write("<br>")
Response.Write("Last Name: ")
Response.Write(lastName)
%>
</body>
</html>
This would produce:
Numeric Data Types An Integer An integer is a whole natural number, positive or negative, between -2,147,483,648 and 2,147,483,647. To declare a variable to hold values in that range, use a data type named Integer. Here is an example: <%
Dim NumberOfPages As Integer
%>
Alternatively, you can use the % type character to declare an integral variable. Here is an example: <%
Dim NumberOfPages%
%>
To convert a value to an integer, use CInt(): enter the value or the expression in the parentheses of CInt(). If the conversion is successful, CInt() produces an integral value. After declaring an Integer variable, to initialize the variable with a regular natural number, simply assign it that number. Here is an example: <%
Dim NumberOfPages As Integer
NumberOfPages = 846
%>
When initializing the variable, to indicate that the value must be considered as an integer, you can type i or I on the right side of the value. Here is an example: <%
Dim NumberOfPages As Integer
NumberOfPages = 846I
%>
Another type of integral number is referred to as hexadecimal. To initialize an integer variable with a hexadecimal number, start the value with &h or &H. Here is an example: <%
Dim NumberOfStudents As Integer
NumberOfStudents = &H4A26EE
%>
Besides the regular natural or the hexadecimal numbers, another type of integer is referred to as octal. This is a technique used to represent a natural number using a combination of 0, 1, 2, 3, 4, 5, 6, and 7. To use such a value, start it with &o or &O (the letter O and not the digit 0). Here is an example: <%
Dim NumberOfStudents As Integer
NumberOfStudents = &O4260
%>
An Unsigned Integer An unsigned integer is a number between 0 and 4294967295. To declare a variable that can a large positive number, use the UInteger data type. To convert a value to an unsigned short integer, use CUInt() by entering the value or the expression in the parentheses. A Byte A byte is a gsmall number between 0 and 255. To declare a variable for a small number, use the Byte data type. A Byte variable is initialized with 0. Otherwise, to initialize it, you can assign it a small number from 0 to 255. To convert a value to a Byte value, use CByte(). To do this, enter the value or the expression in the parentheses of CByte(). If the conversion is successful, CByte() produces a Byte value. A Signed Byte A signed byte is a small number between -127 and 128. To declare a variable for such a number, you can use the SByte data type. To convert a value to an SByte value, use CSByte(). A short integer is a natural number in the range of -32768 to 32767. To declare a variable that can hold such a number, you can use the Short data type. Here is an example: <%
Dim MusicTracks As Short
MusicTracks = 16
%>
To indicate that the number must be treated as a Short and not another type of value, type s or S on the right of the initializing value. Here is an example: <%
Dim MusicTracks As Short
MusicTracks = 16S
%>
To convert a value to a short integer, use CShort() by entering the value or the expression in the parentheses of CShort(). If the conversion is successful, CShort() produces a Short value. An unsigned short integer is a natural number between 0 and 65535. To declare a variable that would hold a short positive number, use the UShort data type. To convert something to an unsigned short integer, put it in the parentheses of CUShort(). A long integer is a very large natural number between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. To declare a variable that can hold a very large natural number, use the Long data type. Here is an example: <% Dim Population As Long %> Instead of the AS Long expression, as an alternatively, you can also use the @ symbol as the type character to declare a Long variable. Here is an example: <%
Dim Population@
%>
To convert a value to a long integer, use CLng(). To do this, enter the value or the expression in the parentheses of CLng(). If the conversion is successful, CLng() produces a Long integer value. Like all integers, a Long variable is initialized, by default, with 0. After declaring a Long variable, you can initialize with the necessary natural number. To indicate that the value must be treated as Long, type l or L on the right side of the number. Here is an example: <%
Dim Population@
Population@ = 9793759L
%>
Because Long is primarily an integral like the Integer data type, you can also initialize it using a decimal, a hexadecimal, or an octal value. An Unsigned Long Integer An unsigned long integer is a number between 0 and 18446744073709551615. To declare a variable that can hold such a very large natural positive number, use the ULong data type. To convert a value to an unsigned long integer, use CULng(). Floating-Point Numbers Single-Precision A decimal number is said to have single precision if it ranges between -3.402823e38 and -1.401298e-45 if the number is negative, or 1.401298e-45 and 3.402823e38 if the number is positive. To declare a variable for such a number, use the Single data type. Here is an example: <%
Dim Distance As Single
%>
Instead of the AS Single expression, you can use the ! symbol as the type character. Here is an example: <%
Dim Distance!
%>
The ! symbol can also be used in other scenarios in the Visual Basic language. Whenever you use it, make sure the word that comes to its right is not the name of a variable. When initializing the variable, to indicate that the value is for a decimal number with single-precision, type f or F on the right side of the number. Here is an example: <%
Dim Distance!
Distance! = 195.408f
%>
To convert a string to a long integer, use CSng(). Enter the value or the expression in the parentheses of CSng(). If the conversion is successful, CSng() produces a Single value. Double-Precision A floating-point number with double-precision is a number between 1.79769313486231e308 and 4.94065645841247e324 if it is negative or between 1.79769313486231E308 and 4.94065645841247E324 if the number is positive. To let you declare a variable for such a number, the Visual Basic language provides the Double data type. Here is an example: <%
Dim TempFactor As Double
%>
If you want, you can omit the AS Double expression but use the # symbol instead to declare a Double variable. This can be done as follows: <%
Dim TempFactor#
%>
After declaring a Double variable, you can initialize it with the needed value. To indicate that the variable being used must be treated with double precision, enter r or R on its right side. Here is an example: <%
Dim TempFactor#
TempFactor# = 482r
%>
To convert a value to a double-precision number, call CDbl() by entering the value or the expression in the parentheses of CDbl(). If CDbl() succeeds, it produces a Double value. Decimal A floating-point number with decimal precision is a number between ±1.0 x 10−28 to ±7.9 x 1028 with a precision of 28 to 29 digits. To declare a variable for such a number, use the Decimal keyword. When initializing the variable, to indicate that the value must be treated as decimal, add a d or D to the right side of the value. Here is an example: <%
Dim DistanceBetweenPlanets As Decimal
DistanceBetweenPlanets = 592759797549D
%>
To convert a value or an expression to Decimal, you can call CDec(). Number Formatting Besides using CStr() to convert a value to a string, you can use ToString(). In this case, type the name of the variable followed by ToString(). As an option, in the parentheses, type one of the following letters:
Topics on Declaring and Using Variables Object An object can be any type of value you want to use in your program. To let you declare a variable for a vague value, Visual Basic provides a data type named Object. To convert a value or an expression to the Object type, you can use CObj(). Constants A constant is a value that doesn't change. There are two types of constants you will use: those supplied to you and those you define yourself. To create a constant to use in your code, type the Const keyword followed by a name for the variable, followed by the assignment operator "=", and followed by the value that the constant will hold. Here is an example: <%
Const DateOfBirth As String = "12/5/1974"
%>
When creating a constant, if its value supports a type character, instead of using the As keyword, you can use that type character. Here is an example: <%
Const Temperature% = 52
%>
As mentioned earlier, the second category of constants are those that are built in the Visual Basic language. Because there are many of them and used in different circumstances, when we need one, we will introduce and then use it. |