Home

Variables and Data Types

 

Variable Declaration With Type

 

Introduction

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

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 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.

Numeric Data Types

 

Introduction

The Visual Basic language supports various type of natural numbers, called integers.

Practical LearningPractical Learning: Introducing Variables

  1. Start Microsoft Visual Web Developer or Microsoft Visual Studio 2008 Professional 
  2. To create a new web site, on the main menu, click File -> New Website or New -> Web Site...
  3. In the Language to Visual Basic.
    Set the name to gdc1
  4. Click OK

A Byte

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 LearningPractical Learning: Using Bytes

  1. 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>
  2. To execute the program, press Ctrl + F5
  3. Enter the number of shirts as 4 and the number of pants as 2. This would produce:
     
    Georgetown Dry Cleaning Services
  4. Return to your programming environment

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. 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.

A Short Integer

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 LearningPractical Learning: Using Unsigned Short Integers

  1. 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>
  2. Save the file
  3. Return to the browser and refresh
  4. Enter the number of shirts as 2, the number of pants as 5, and the number of other items as 3. This would produce:
     
    Georgetown Dry Cleaning Services
  5. Return to your programming environment

An Integer

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>

An Unsigned Integer

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

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

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()

 
 
 
 

Floating-Point Numbers

 

Introduction

A real number is one that displays a decimal part made of one or two sections separated by a Decimal Separator or Decimal Symbol. 

Single-Precision

A single precision decimal number is a value that can range from –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:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Distance As Single
%>

</body>
</html>

Instead of the AS Single expression, you can use the ! symbol as the type character. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Distance!
%>

</body>
</html>

A Single variable is initialized with 0. After declaring a Single variable, you can declare it with the necessary value. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Distance!

    Distance! = 195.408
    Response.Write("Distance: " & Distance)
%>

</body>
</html>

To indicate that the variable must be treated as a single-precision decimal number, type f or F on the right side of the number. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Distance!

    Distance! = 195.408f 
%>

</body>
</html>

To convert a string to a long integer, call 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 double-precision decimal variable is one that can hold a large number between 1.79769313486231e308 and –4.94065645841247e–324 if the number is negative or between 1.79769313486231E308 and 4.94065645841247E–324 if the number is positive. To declare a variable that can store a large decimal number with precision, use the Double keyword.

Here is an example of declaring a Double variable:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim TempFactor As Double
%>

</body>
</html>

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:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim TempFactor#
%>

</body>
</html>

A double-precision variable is initialized with a 0 value. After declaring a Double variable, you can initialize it with the needed value. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim TempFactor#

    TempFactor# = 482
    Response.Write("Temperature: " & TempFactor & " Degrees")
%>

</body>
</html>

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:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim TempFactor#

    TempFactor# = 482r
    Response.Write("Temperature: " & TempFactor & " Degrees")
%>

</body>
</html>

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.

Practical Learning: Using Double Precision Variables

  1. To use the double-precision variables, 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
        Dim UnitPriceShirts As Double
        Dim UnitPricePants As Double
        Dim UnitPriceOtherItems As Double
        Dim SubTotalShirts As Double
        Dim SubTotalPants As Double
        Dim SubTotalOtherItems As Double
    
        UnitPriceShirts = 1.25
        UnitPricePants = 1.85
        UnitPriceOtherItems = 2.25
        
        Shirts = CByte(InputBox("Enter Number of Shirts"))
        Pants = CByte(InputBox("Enter Number of Pants"))
        OtherItems = CUShort(InputBox("Enter Number of Other Items"))
    
        SubTotalShirts = Shirts * UnitPriceShirts
        SubTotalPants = Pants * UnitPricePants
        SubTotalOtherItems = OtherItems * UnitPriceOtherItems
    %>
    
    <%    
        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>")
        Response.Write("<td width=100>" & UnitPriceShirts & "</td>")
        Response.Write("<td width=100>" & SubTotalShirts & "</td></tr><tr>")
        Response.Write("<td width=100 align=left><b>Pants</b></td>")
        Response.Write("<td width=100>" & Pants & "</td>")
        Response.Write("<td width=100>" & UnitPricePants & "</td>")
        Response.Write("<td width=100>" & SubTotalPants & "</td></tr><tr>")
        Response.Write("<td width=100 align=left><b>Other Items</b></td>")
        Response.Write("<td width=100>" & OtherItems & "</td>")
        Response.Write("<td width=100>" & UnitPriceOtherItems & "</td>")
        Response.Write("<td width=100>" & SubTotalOtherItems & "</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>
  2. Save the file
  3. Return to the browser and refresh
  4. Enter 4 for the shirts, 2 for the pants, and 3 for other items
  5. Return to your programming environment

Decimal

The decimal data type can be used to declare a variable that would hold significantly large values that can be stored in a combination of 128 bits. You declare such a variable using the Decimal keyword. The values stored in a decimal variable can range from ±1.0 × 10−28 to ±7.9 × 1028 with a precision of 28 to 29 digits. Because of this high level of precision, the Decimal data type is suitable for currency values.

Like those of the other numeric types, by default, a variable declared as Decimal is initialized with 0. After declaring a decimal variable, you can initialize it with a natural number. To indicate to the compiler to reserve space enough to store a decimal value, add a d or D to the right side of the value. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim DistanceBetweenPlanets As Decimal

    DistanceBetweenPlanets = 592759797549D
%>

</body>
</html>

To convert a value or an expression to Decimal, you can call CDec().

Characters and Strings

 

Characters

To declare a variable that would hold a character, use the Char data type. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Letter As Char
%>

</body>
</html>

When declaring a Char variable, if you don't initialize it, it gets an empty character. Otherwise, after declaring a Char variable, you can initialize it with a single character included in double-quotes. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Letter As Char

    Letter = "W"
%>

</body>
</html>

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:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Letter As Char

    Letter = "W"c
%>

</body>
</html>

To convert a value to Char, you can call CChar().

Strings

A string is an empty text, a letter, a word or a group of words considered "as is". To declare a string variable, you can use the String data type. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Sentence As String
%>

</body>
</html>

If you want, you can replace the AS String expression with the $ symbol when declaring a string variable. This can be done as follows:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Sentence$
%>

</body>
</html>

After declaring a String variable, by default, it gets an empty string. To initialize it with a string of your choice, include the string between double quotes. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Sentence$

    Sentence$ = "He called me"
%>

</body>
</html>

If you want to include a double-quote character in the string, you can double it. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Sentence$

    Sentence$ = "Then she said, ""I don't love you no more."" and I left"
%>

<%
    Response.Write(Sentence)
%>

</body>
</html>

This would produce:

String

To convert a value to a string, call CStr() and enter the value or the expression in its parentheses. If the value is an appropriate date or time, CStr() would produces a string that represents that date or that time value.

Dates and Times

To declare a variable that can hold either date values, time values, or both, you can use the Date data type. After the variable has been declared, you will configure it to the appropriate value. Here are two examples:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim DateOfBirth As Date
    Dim KickOffTime As Date
%>

</body>
</html>

As mentioned above, the Date data type is used for both date and time. There are rules you must observe when dealing with date and time values. The rules are defined in the Regional Settings Properties of the Control Panel of the computer on which the application is run. The rules for the dates are defined in the Date property page:

Regional

The rules for the time values are defined in the Time property page:

Options

By default, a Date variable is initialized with January 1st, 0001 at midnight as the starting value. After declaring a Date variable, you can initialize it with an appropriate date or time or date and time value. The value can be included in double-quotes. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim DateOfBirth As Date

    DateOfBirth = "08/14/1982"
%>

<%
    Response.Write("Date of Birth: " & DateOfBirth)
%>

</body>
</html>

A Date value can also be included between two # signs. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim DateOfBirth As Date
    Dim KickOffTime As Date

    DateOfBirth = "08/14/1982"
    KickOffTime = #6:45:00 PM#
%>

<%
    Response.Write("Date of Birth: " & DateOfBirth)
    Response.Write("<br>Kick off Time: " & KickOffTime)
%>

</body>
</html>

To convert a value to a date or a time value, write an appropriate date or a recognizable time in the parentheses of CDate(). If the value is appropriate, CDate() produces a Date value.

 
 
   
 

Previous Copyright © 2009-2013 FunctionX, Inc. Next