Home

Variables and Data Types

 

Variables

 

Introduction

A web page whose main purpose is to display regular text and images to the browser is suited for HTML tags only. To make the page more interesting, you and your users can exchange information, as illustrated in the previous lesson. For example, you may want your users to submit values to the server, let the server either store those values or analyze them and send some results to the user. This interact usually means that, in the beginning, you would not know the type of value(s) that one visitor would submit as opposed to another user. Based of this, you must prepare storage areas on the user or the server's computer memory to temporarily hold the values used during a visitor's interaction with your web page. This type of storage area is called a variable.

As there are different types of values used in an application, there are also different requirements to store those values. As introduced in our VBScript Lesson 4, to prepare a storage area, you type the Dim keyword followed by a name.

The Name of a Variable

To specify the name of a variable, there are rules you must follow:

  • The name of a variable must start with either a letter or an underscore. A name can have only one letter but if you start it with an underscore, then the underscore must be followed by a letter
  • After the first character as an underscore or a letter, the name can contain letters, digits, or underscores in any combination
  • The name must not contain space but it can be made of various words as long as these words are concatenated (added together to produce one word)
  • The name must not contain any symbol other than letters, digits, or underscores 

After respecting these rules, you can adopt a naming convention that suits you.

 

Data Types

Unlike some other languages, VBScript recognizes only a data type called Variant when declaring a variable using the following formula:

Dim VariableName

The Variant data type can be used in place of any type. After declaring the variable, its type is still not know. If you had requested a value from the user, before involving it in a calculation, you should first convert it to the appropriate type. Fortunately, VBScript provides the necessary means (functions) to perform this conversion.

 

String

A string is an empty space, a character, or a group of characters. When you present a control to the user who is supposed to type in it, the value that the user enters is primarily a string, even if you requested a character or a number. Here is an example of declaring a variable that would hold a string:

Dim Country

To initialize the string, you can pass a double-quoted value to it. Here is an example:

<%@ Language="VBScript" %>
<html>
<head>
<title>Active Server Pages Tutorials</title>
</head>
<body>
<h1>Lesson 4: Variables and Data Types</h1>

<p><b>Lecturer:</b> Benjamin Jacobson</b>
<br>
<b>Time Allocated:</b> 74 minutes</p>

<p>This lesson introduces the use of the computer memory to store some 
values that would be used while a visitor is interacting with a web page.</p>

<%
  Dim Country

  Country = "Argentina"
  Response.Write(Country)
%>
<h3>Enjoy</h3>
</body>
</html>

To convert a value or an expression into a string, you can use the CStr() function. To use it, pass the value or the expression in the parentheses of this function.

 

Practical Learning: Using Strings

  1. Start a new file in your text editor and type the following:
     
    <%@ Language="VBScript" %>
    <html>
    <head>
    <title>La Familia: Membership</title>
    </head>
    <body>
    
    <h1>Membership Application</h1>
    
    <%
      Dim strFirstName
      Dim strLastName
      Dim strFullName
    
      strFirstName = Request.Form("txtFirstName")
      strLastName  = Request.Form("txtLastName")
      strFullName  = strLastName + ", " + strFirstName
      Response.Write("<b>New Member:</b> ")
      Response.Write(strFullName) %>
    
    </body>
    </html>
    
    </body>
    </html>
  2. Save it as members.asp in your La Familia folder
  3. Start a new file and type the following:
     
    <%@ Language="VBScript" %>
    <html>
    <head>
    <title>La Familia: Membership</title>
    </head>
    <body>
    
    <h1>Membership Application</h1>
    <form method="POST" action="members.asp">
      <table border="0" width="357">
        <tr>
          <td width="194">First Name:</td>
          <td width="8"><input type="text" name="txtFirstName" size="14"></td>
          <td width="135"></td>
        </tr>
        <tr>
          <td width="194">Last Name:</td>
          <td width="8"><input type="text" name="txtLastName" size="14"></td>
          <td width="135"><input type="submit" value="Submit" name="btnSubmit"></td>
        </tr>
      </table>
    </form>
    
    </body>
    </html>
    
    </body>
    </html>
  4. Save it as membership.asp in your La Familia folder
  5. Access your browser and change its address to http://localhost/family/membership.asp
  6. Press Enter. Type a first and a last names
     
  7. Click Submit
     
  8. Return to your text editor

Natural Numbers

A number is referred to as natural when it is considered as a whole. Examples are 2, 408, or 732945793. Active Server Pages supports various types of natural numbers.

A byte is a small positive number that is less than or equal to 255. After getting a value or an expression, to convert it to a Byte, you can use the CByte() function. To do this, pass the value or the expression in the parentheses of CByte().

An integer is a natural number whose value can range from -32768 to 2,147,484,647. After getting a value or an expression that represents a natural number, to convert it to a natural number, you can use the CInt() function by entering the value or the expression in the parentheses of this function.

If the value is significantly large beyond the range of an integer, to convert it, use the CLng() function and pass the value or the expression in the parentheses of this function.

 

Practical Learning: Using Natural Numbers

  1. In Windows Explorer or My Computer, create a folder named Small Business
  2. As done in the first lesson, in Internet Information Services, in the Default Web Site, create a virtual directory whose alias is smb and whose path is the Small Business folder 
  3. Start a new file in your text editor and type the following:
     
    <%@ Language="VBScript" %>
    <html>
    
    <head>
    
    <title>CD Publisher</title>
    </head>
    
    <body>
    
    <h1>CD Publisher</h1>
    
    <form method="GET" action="cdpublisher1.asp">
      <table border="0" width="398">
        <tr>
          <td width="214">Number of CDs Ordered:</td>
          <td width="82"><input type="text" name="txtQuantity" size="10"
            value=<% =Request.QueryString("txtQuantity") %> >
          </td>
          <td width="82"><input type="submit" value="Calculate" name="btnCalculate"></td>
        </tr>
        <tr>
          <td>Each CD will cost</td>
          <td width="76"><input type="text" name="txtUnitPrice" size="10"
            value=<% =Request.QueryString("txtUnitPrice") %> ></td>
          <td width="75"></td>
        </tr>
        <tr>
          <td width="214">And the total price is:</td>
          <td width="82">
    
            <input type="text" name="txtTotalPrice" size="10" value=
    <% =CInt(Request.QueryString("txtQuantity")) * Request.QueryString("txtUnitPrice") %> >
            
          </td>
          <td width="82"><input type="reset" value="Start New Order" name="btnReset"></td>
        </tr>
      </table>
    </form>
    
    </body>
    
    </html>
  4. Save the file as cdpublisher1.asp in your Small Business folder
  5. Access your browser
  6. Change its address to http://localhost/smp/cdpublisher1.asp and press Enter
  7. Enter a quantity of 48 and a unit price of 6.12 then click Calculate
     
  8. Return to your text editor
 

Decimal Numbers

A number is referred to as decimal or real when it contains a fractional part. Such a number is considered in two sections separated by a symbol that is referred to as the Decimal Separator or Decimal Symbol. This symbol is different by language, country, group of languages, or group of countries. In US English, this symbol is the period as can be verified from the Regional (and Language) Settings of the Control Panel:

Regional Options

On both sides of the Decimal Symbol, digits are used to specify the value of the number. The number of digits on the right side of the symbol determines how much precision the number offers.

A Single variable can store real numbers that range from ±1.5 × 10−45 to ±3.4 × 1038 with a precision of 7 digits in 32 bits. Here is an example of declaring a variable that would hold a decimal number:

Dim number

To initialize a Single variable, you can assign it a natural number or a number that includes the decimal separator. Here is an example:

<%
    Dim number
    
    number = 128.68
    Response.Write(number)
%>

To convert a value to Single, you can use the CSng() function.

When a variable is larger than the Single can handle and requires more precision, it is referred to as Double. A Double value can store very large numbers ranging from ±5.0 × 10−324 to ±1.7 × 10308 with a precision of 15 or 16 digits. Here is an example of declaring a variable for it:

Dim distance

To convert a value to Double, you can use the CDbl() function.

 

Practical Learning: Using Real Numbers

  1. To convert the unit price to Double, change the cdpublisher1.asp file as follows:
     
    <%@ Language="VBScript" %>
    <html>
    
    <head>
    
    <title>CD Publisher</title>
    </head>
    
    <body>
    
    <h1>CD Publisher</h1>
    
    <form method="GET" action="cdpublisher1.asp">
      <table border="0" width="398">
        <tr>
          <td width="214">Number of CDs Ordered:</td>
          <td width="82"><input type="text" name="txtQuantity" size="10"
            value=<% =Request.QueryString("txtQuantity") %> >
          </td>
          <td width="82"><input type="submit" value="Calculate" name="btnCalculate"></td>
        </tr>
        <tr>
          <td>Each CD will cost</td>
          <td width="76"><input type="text" name="txtUnitPrice" size="10"
            value=<% =Request.QueryString("txtUnitPrice") %> ></td>
          <td width="75"></td>
        </tr>
        <tr>
          <td width="214">And the total price is:</td>
          <td width="82">
    
            <input type="text" name="txtTotalPrice" size="10" value=
    <% =CInt(Request.QueryString("txtQuantity")) * CDbl(Request.QueryString("txtUnitPrice")) %> >
            
          </td>
          <td width="82"><input type="reset" value="Start New Order" name="btnReset"></td>
        </tr>
      </table>
    </form>
    
    </body>
    
    </html>
  2. Save the file
  3. Return to your browser, refresh it, then enter a new quantity and a new unit price before clicking Calculate
 

Date and Time

Active Server Pages provides support for dates and times values as their own data type. Here is an example of declaring a variable used to hold a date or a time value:

Dim StartDate

To initialize a date or time variable, include the value between two # signs and assign that value to the variable. To specify a date value, use the formula #M/d/yyyy#. To specify a time value, you can use any variance of #HH:MM AM/PM# 

Here are three examples:

<%@ Language="VBScript" %>
<html>
<head>
<title>Active Server Pages Tutorials</title>
</head>
<body>
<h1>Lesson 4: Variables and Data Types</h1>

<p><b>Lecturer:</b> Benjamin Jacobson</b>
<br>
<b>Time Allocated:</b> 74 minutes</p>

<p>This lesson introduces the use of the computer memory to store some 
values that would be used while a visitor is interacting with a web page.</p>

<%
    Dim StartDate
    Dim StartTime
    Dim StartDateAndTime

    StartDate = #04/22/2004#
    StartTime = #10:42 AM#
    StartDateAndTime = #04/22/2004 10:42 AM#

    Response.Write(StartDate)
    Response.Write("<br>")
    Response.Write(StartTime)
    Response.Write("<br>")
    Response.Write(StartDateAndTime)
%>
<h3>Enjoy</h3>
</body>
</html>

This would produce:

 
 

Previous Copyright © 2005-2016, FunctionX Next