Home

VBScript Variables

 

VBScript Variables

 

Introduction

A variable is an amount of memory space reserved to store part of the data used in your code. To use such a memory space, you must first let the browser know that you would need it. For the browser to reserve such a memory space for you, and to use it eventually, you must give it a name. The memory space that is reserved will be used to receive some values from your application and to make such values available when your code needs them. The contents of this reserved memory space can change or vary regularly. For example, if you create a web page that acts as an employment application and include a text box for the first name, the content of the first name text box will be different depending on each visitor. The memory space that stores the first name of the visitors will vary from one visitor to another. For this reason, it is called a variable.

The name of the variable:

  • must start with a letter or an underscore character
  • after the first character, can contain letters, digits, and underscores
  • cannot contain empty space

When naming your variables, you should be a little explicit. A name such as d intended to designate a date hired is difficult to figure out. On the other hand, if you plan to use a variable that represents a customer’s address, you can just call the variable address.

For our lessons, sometimes we will start the names of variables in uppercase. Instead of address, we may use Address; instead of customers, we may use Customers. If the name is a combination of words, such as firstname, we may start the first letter of each name in uppercase. For example, instead of studentname, we may use StudentName; instead of dateofbirth, we may use DateOfBirth. Various programmers use different naming conventions. For example, some programmers use the underscore to append two names of a variable. You are free to adopt which ever convention suits you.

Practical Learning: Introducing Variables

  1. Start Notepad or your text editor
  2. In the empty file, type the following:
     
    <html>
    <head>
    <title>VBScript Lessons</title>
    
    </head>
    
    <body>
    
    <h1>Employee Payroll</h1>
    <form name="frmPayroll">
      <table border="0" width="504">
        <tr>
          <td width="144">First Name:</td>
          <td width="101" colspan="2"><input type="text" name="txtFirstName" size="10"></td>
          <td width="239" colspan="5"></td>
        </tr>
        <tr>
          <td width="144">Last Name:</td>
          <td width="101" colspan="2"><input type="text" name="txtLastName" size="10"></td>
          <td width="239" colspan="5"></td>
        </tr>
        <tr>
          <td width="144">Days:</td>
          <td width="49">Mon</td>
          <td width="46">Tues</td>
          <td width="43">Web</td>
          <td width="43">Thurs</td>
          <td width="43">Fri</td>
          <td width="43">Sat</td>
          <td width="43">Sun</td>
        </tr>
        <tr>
          <td width="144">Hours:</td>
          <td width="49"><input type="text" name="txtMonday" size="5" value="0.00"></td>
          <td width="46"><input type="text" name="txtTuesday" size="5" value="0.00"></td>
          <td width="43"><input type="text" name="txtWednesday" size="5" value="0.00"></td>
          <td width="43"><input type="text" name="txtThursday" size="5" value="0.00"></td>
          <td width="43"><input type="text" name="txtFriday" size="5" value="0.00"></td>
          <td width="43"><input type="text" name="txtSaturday" size="5" value="0.00"></td>
          <td width="43"><input type="text" name="txtSunday" size="5" value="0.00"></td>
        </tr>
        <tr>
          <td width="144">Hourly Salary:</td>
          <td width="101" colspan="2">
            <input type="text" name="txtHourlySalary" size="13" value="6.50"></td>
          <td width="239" colspan="5"></td>
        </tr>
        <tr>
          <td width="144">Weekly Hours:</td>
          <td width="101" colspan="2">
            <input type="text" name="txtWeeklyHours" size="13" value="0.00"></td>
          <td width="239" colspan="5">
            <input type="reset" value="ClearAll" name="btnClearAll"></td>
        </tr>
        <tr>
          <td width="144">Weekly Salary:</td>
          <td width="101" colspan="2">
            <input type="text" name="txtWeeklySalary" size="10" value="0.00"></td>
          <td width="239" colspan="5">
            <input type="submit" value="Calculate" name="btnCalculate"></td>
        </tr>
      </table>
    </form>
    
    </body>
    </html>
  3. Save the file as payroll1.htm in your VBScript Lessons folder
  4. Preview the file in the browser
     
  5. Return to your text editor
 

Variable Declaration

To use a variable in your script, you can just type it. Here is an example:

<SCRIPT LANGUAGE="VBScript">
  NumberOfStudents
</SCRIPT>

With such a variable, you can perform any of the operations we will learn shortly. Once the variable has been used somewhere, the browser reserves a space in memory for it. If you use another or new name for a variable in the same script, if the new variable has a different name than a former variable, the browser would reserve a new space in memory space for it. You can continue introducing variables like that as your script needs them.

Be careful not to mistakenly type the name of variable that you want to reuse in your program. For example, if you have a variable named NumberOfStudents in your script, then find out that you want to use it in an operation, if you type NbrOfStudents instead of NumberOfStudents, since both names are different, a new memory space would be reserved for NbrOfStudents because the browser would consider that NbrOfStudents is a new variable. Trying to use both variables as one would lead to unpredictable results. To avoid this confusion, VBScript provides two alternatives. Before using a variable, you can first tell the browser that you have a variable you want to use. By doing this, the browser would be a "witness" that a certain variable exists. This is (still) not a bulletproof solution but can help to improve your code.

Letting the interpreter know about a variable is referred to as declaring the variable. To declare a variable, use the Dim keyword followed by a name for the variable. Here are examples of declaring variables:

<Script Language="VBScript">
  Dim FirstName
  Dim HiredDate
  Dim HourlySalary
</Script>

Although the Dim keyword is used to declare a variable, it can be used for any kind of variable. It can be used to declare a variable that represents a natural number (integer), a decimal number (floating-point or double-precision number), a Boolean value, or a string.

If you need to use many variables, you can declare more than one on the same line. To do this, separate them with a comma. Here is an example:

<Script Language="VBScript">
  Dim FullName, EmailAddress
  Dim Address, City, State, ZIPCode
</Script>

Using the Dim keyword to declare a variable doesn't completely eliminate name duplication of variables. A better alternative that VBScript proposes is to impose it upon your code that no variable should be used if it has not been previously declared. Using this technique, if you reference a variable without declaring it, the browser would display an error.

To impose the rule that each variable must first be declared before being used, after the opening script tag, type option explicit as follows:

<Script Language="VBScript">
  Option Explicit
  ' Code goes here
</Script>

If you try using or accessing a variable that was not previously declared, you may receive an error.

Practical Learning: Declaring Variables

  1. Declare a few variables in a script as follows:
     
    <html>
    <head>
    <title>VBScript Lessons</title>
    
    <Script Language="VBScript">
      Option Explicit
    
    Dim Mon, Tue, Wed, Thu, Fri, Sat, Sat, Sun
    Dim WeeklyHours, HourlySalary, WeeklySalary
    
    </Script>
    
    </head>
    
    <body>
    
    <h1>Employee Payroll</h1>
    . . . No Change
    
    </body>
    </html>
  2. Save the file
 

Previous Copyright © 2004-2010 FunctionX, Inc. Next