Variable Declaration With Type
|
|
|
To specify the amount of memory that a variable
would use, on the right side of the variable's name, you
type the As keyword followed by the data type. The
formula to declare such a variable is:
Dim VariableName As DataType
|
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 and 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 it can be applied.
The Visual Basic language supports various type of natural numbers,
called integers.
Practical
Learning: Introducing Variables
|
|
- Start Microsoft Visual Web Developer or Microsoft Visual
Studio
2008 Professional
- To create a new web site, on the main menu, click File -> New Website
or New -> Web Site...
- In the Language to Visual Basic.
Set the name to gdc1
- Click OK
A byte is a small natural positive number that ranges
from 0 to 255. A variable of byte type can be used to hold small values
such as a person's age, the number of fingers on an animal, etc.
To declare a variable for a small number, use
the Byte keyword. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim StudentAge As Byte
%>
</body>
</html>
A Byte variable is initialized with 0.
Otherwise, to initialize it, you can assign it a small number from 0 to
255.
There is no type character for the Byte data
type.
To convert a value to a Byte value, you can 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.
Practical
Learning: Using Bytes
|
|
- While the Source file is still opened, change it as follows:
<%@ Page Language="VB"
AutoEventWireup="false"
CodeFile="Default.aspx.vb"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Georgetown Dry Cleaning Services</title>
</head>
<body>
<%
Dim Shirts As Byte
Dim Pants As Byte
Shirts = CByte(InputBox("Enter Number of Shirts"))
Pants = CByte(InputBox("Enter Number of Pants"))
%>
<%
Response.Write("<div align=center><center>")
Response.Write("<table border=0 width=420><tr>")
Response.Write("<td width=100% align=center><h3><b>")
Response.Write("=#= Georgetown Dry Cleaning Services =#=</h3></b></td>")
Response.Write("</tr></table></center></div>")
Response.Write("<div align=center><center>")
Response.Write("<table border=0 width=400><tr>")
Response.Write("<td width=100% bgcolor=#0000FF height=2 colspan=4></td>")
Response.Write("</tr><tr><td width=100 align=left><b>Item Type</b></td>")
Response.Write("<td width=100><b>Qty</b></td>")
Response.Write("<td width=100><b>Unit Price</b></td>")
Response.Write("<td width=100><b>Sub-Total</b></td>")
Response.Write("</tr><tr>")
Response.Write("<td width=100% bgcolor=#0000FF height=1 colspan=4></td>")
Response.Write("</tr><tr><td width=100 align=left><b>Shirts</b></td>")
Response.Write("<td width=100>" & Shirts & "</td><td width=100></td>")
Response.Write("<td width=100></td></tr><tr>")
Response.Write("<td width=100 align=left><b>Pants</b></td>")
Response.Write("<td width=100>" & Pants & "</td><td width=100></td>")
Response.Write("<td width=100></td></tr><tr>")
Response.Write("<td width=100 align=left><b>Other Items</b></td>")
Response.Write("<td width=100></td><td width=100></td>")
Response.Write("<td width=100></td></tr><tr>")
Response.Write("<td width=100% bgcolor=#0000FF height=2 colspan=4></td>")
Response.Write("</tr></table></center></div>")
Response.Write("<div align=center><center>")
Response.Write("<table border=0 width=400><tr><td width=100></td>")
Response.Write("<td width=120 align=left><b>Total Cleaning:</b></td>")
Response.Write("<td width=100></td><td></td>")
Response.Write("</tr><tr><td width=100></td>")
Response.Write("<td width=120 align=left><b>Tax Rate:</b></td>")
Response.Write("<td width=100></td><td></td>")
Response.Write("</tr><tr><td width=100></td>")
Response.Write("<td width=120 align=left><b>Tax Amount:</b></td>")
Response.Write("<td width=100></td><td></td></tr>")
Response.Write("<tr><td width=100></td>")
Response.Write("<td width=120 align=left><b>Net Price</b></td>")
Response.Write("<td width=100></td><td></td>")
Response.Write("</tr></table></center></div>")
%>
</body>
</html>
- To execute the program, press Ctrl + F5
- Enter the number of shirts as 4 and the number of pants as 2. This would produce:
- Return to your programming environment
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. This can be done as follows:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Temperature As SByte
%>
</body>
</html>
To convert a value to an SByte value, use CSByte().
There is no type character for the SByte data
type.
An integer is a natural number larger than the Byte. To declare a variable that can hold natural
numbers in the range of -32768 to 32767, you can use the Short data
type.
Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim MusicTracks As Short
%>
</body>
</html>
After declaring the variable, you can initialize
it with a value between
-32768 and 32767.
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:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim MusicTracks As Short
MusicTracks = 16S
Response.Write("This album contains " & MusicTracks & " tracks.")
%>
</body>
</html>
To convert a value to a short integer, you can
use CShort() by entering the value or the expression
in the parentheses of CShort(). If the conversion is successful, CShort()
produces a Short value.
There is no type character for the Short data
type.
An Unsigned Short Integer
|
|
As mentioned above, a short integer can be either negative
or positive. If you want to use a relatively small number that must be positive,
you must store it as an unsigned short integer. An unsigned short integer is a
natural number between 0 and 65535.
To declare a variable that would hold a short positive
number, you can use the UShort data type. Here is an
example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim TotalNumberOfStudents As UShort
%>
</body>
</html>
There is no type character for the UShort data
type.
To convert something to an unsigned short integer, put
in the parentheses of CUShort().
Practical
Learning: Using Unsigned Short Integers
|
|
- To use unsigned short integers, change the file as follows:
<%@ Page Language="VB"
AutoEventWireup="false"
CodeFile="Default.aspx.vb"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Georgetown Dry Cleaning Services</title>
</head>
<body>
<%
Dim Shirts As Byte
Dim Pants As Byte
Dim OtherItems As UShort
Shirts = CByte(InputBox("Enter Number of Shirts"))
Pants = CByte(InputBox("Enter Number of Pants"))
OtherItems = CUShort(InputBox("Enter Number of Other Items"))
%>
<%
Response.Write("<div align=center><center>")
Response.Write("<table border=0 width=420><tr>")
Response.Write("<td width=100% align=center><h3><b>")
Response.Write("=#= Georgetown Dry Cleaning Services =#=</h3></b></td>")
Response.Write("</tr></table></center></div>")
Response.Write("<div align=center><center>")
Response.Write("<table border=0 width=400><tr>")
Response.Write("<td width=100% bgcolor=#0000FF height=2 colspan=4></td>")
Response.Write("</tr><tr><td width=100 align=left><b>Item Type</b></td>")
Response.Write("<td width=100><b>Qty</b></td>")
Response.Write("<td width=100><b>Unit Price</b></td>")
Response.Write("<td width=100><b>Sub-Total</b></td>")
Response.Write("</tr><tr>")
Response.Write("<td width=100% bgcolor=#0000FF height=1 colspan=4></td>")
Response.Write("</tr><tr><td width=100 align=left><b>Shirts</b></td>")
Response.Write("<td width=100>" & Shirts & "</td><td width=100></td>")
Response.Write("<td width=100></td></tr><tr>")
Response.Write("<td width=100 align=left><b>Pants</b></td>")
Response.Write("<td width=100>" & Pants & "</td><td width=100></td>")
Response.Write("<td width=100></td></tr><tr>")
Response.Write("<td width=100 align=left><b>Other Items</b></td>")
Response.Write("<td width=100>" & OtherItems & "</td><td width=100></td>")
Response.Write("<td width=100></td></tr><tr>")
Response.Write("<td width=100% bgcolor=#0000FF height=2 colspan=4></td>")
Response.Write("</tr></table></center></div>")
Response.Write("<div align=center><center>")
Response.Write("<table border=0 width=400><tr><td width=100></td>")
Response.Write("<td width=120 align=left><b>Total Cleaning:</b></td>")
Response.Write("<td width=100></td><td></td>")
Response.Write("</tr><tr><td width=100></td>")
Response.Write("<td width=120 align=left><b>Tax Rate:</b></td>")
Response.Write("<td width=100></td><td></td>")
Response.Write("</tr><tr><td width=100></td>")
Response.Write("<td width=120 align=left><b>Tax Amount:</b></td>")
Response.Write("<td width=100></td><td></td></tr>")
Response.Write("<tr><td width=100></td>")
Response.Write("<td width=120 align=left><b>Net Price</b></td>")
Response.Write("<td width=100></td><td></td>")
Response.Write("</tr></table></center></div>")
%>
</body>
</html>
- Save the file
- Return to the browser and refresh
- Enter the number of shirts as 2, the number of pants as 5, and the
number of other items as 3. This would produce:
- Return to your programming environment
If you want a variable to hold values larger than
the Short data type can accommodate, you can use
the Integer data type to declare it. A variable declared with the Integer data
type can hold a value between -2,147,483,648 and
2,147,483,647. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim NumberOfPages As Integer
%>
</body>
</html>
Alternatively, you can use the %
type character to declare an integral variable. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim NumberOfPages%
%>
</body>
</html>
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, it is initialized with 0. To initialize the
variable with a regular natural number, you can simply assign it that
number. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim NumberOfPages As Integer
NumberOfPages = 846
Response.Write("This book contains " & NumberOfPages & " pages.")
%>
</body>
</html>
The Visual Basic language considers
three types of integer values. Instead of just assigning a regular natural
number, to indicate that the value must be considered as an integer, you
can enter i or I on the right side of the value. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim NumberOfPages As Integer
NumberOfPages = 846I
%>
</body>
</html>
To initialize an integer variable with a hexadecimal
number, start the value with &h or &H. Here is an
example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim NumberOfStudents As Integer
NumberOfStudents = &H4A26EE
Response.Write("School Current Enrollment: " & NumberOfStudents & " students.")
%>
</body>
</html>
To initialize the variable with an octal value, start it with &o or &O (the letter O and not
the digit 0). Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim NumberOfStudents As Integer
NumberOfStudents = &O4260
Response.Write("School Current Enrollment: " & NumberOfStudents & " students.")
%>
</body>
</html>
If you want a variable to hold only positive integers between 0 and
4294967295, declare it using UInteger. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Population As UInteger
%>
</body>
</html>
You can initialize the variable using a positive
integer in decimal, hexadecimal, or octal format.
To convert a value to an unsigned short integer,
use CUInt() by entering the value or the expression
in the parentheses.
There is no type character for the UInteger data
type.
A long integer is a very large natural number that is
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:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Population As Long
%>
</body>
</html>
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:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Population@
%>
</body>
</html>
To convert a value to a long integer, you can 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. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Population@
Population@ = 9793759
Response.Write("Country Population: " & Population)
%>
</body>
</html>
To indicate that the value must be treated as Long,
type l (lowercase L) or L on the right side of the number. Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Population@
Population@ = 9793759L
%>
</body>
</html>
Because Long is primarily an integral type like the
Integer
data type, you can also initialize it using a decimal,
a hexadecimal, or an octal value. Here is an example of a variable
initialized with a hexadecimal value:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim Population@
Population = &HFF42AD
Response.Write("Country Population: " & Population)
%>
</body>
</html>
An unsigned
long integer is a very large positive number between 0 and 18446744073709551615.
To declare a variable that can hold such a positive number, use the ULong
data type.
Here is an example:
<%@ Page Language="VB" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>
<%
Dim DistanceBetweenBothPlanets As ULong
%>
</body>
</html>
There is no type character for the ULong data
type.
To convert a value to an unsigned long integer, use CULng().