Intranet Journal   Earthweb  
Events Jobs Premium Services Media Kit Network Map E-mail Offers Vendor Solutions Webcasts

   Intranet Journal Subjects
Search Earthweb

Privacy Policy



internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

internet commerce
Be a Commerce Partner
















 

[ Home | Discussion Forum | How Do I... | Lotus Notes Intranets | Microsoft SharePoint | Products | Shopping  ]

free news!


Lesson 2-Working with Variables

A variable is a named location in computer memory that you can use for storage of data during the execution of your scripts. You can use variables to:

  • Store input from the user gathered via your web page
  • Save data returned from functions
  • Hold results from calculations

An Introduction to Variables

Let's look at a simple VBScript example to clarify the use of variables.

Sub cmdVariables_OnClick

  Dim Name

  Name = InputBox("Enter your name: ")

  MsgBox "The name you entered was " & Name

End Sub

The first line of this example defines a sub procedure associated with the click event of a command button named cmdVariables.

On the second line we declare a variable named Name. We are going to use this variable to store the name of the user when it is entered. The third line uses the InputBox function to first prompt for, and then return, the user's name. You will see more of the InputBox function later in this tutorial. The name it returns is stored in the Name variable.

The fourth line uses the MsgBox function to display the user's name. Finally, the sub procedure completes on line five.

Exactly how, and where, variables are stored is not important. What you use them for, and how you use them is important. That is what we will be looking at next.

Declaring Variables

There are two methods for declaring variables in VBScript, explicitly and implicitly. You usually declare variables explicitly with the Dim statement:

Dim Name

This statement declares the variable Name. You can also declare multiple variables on one line as shown below, although it is preferable to declare each variable separately:

Dim Name, Address, City, State

Variables can be declared implicitly by simply using the variable name within your script. This practice is not recommended. It leads to code that is prone to errors and more difficult to debug.

You can force VBScript to require all variables to be explicitly declared by including the statement Option Explicit at the start of every script. Any variable that is not explicitly declared will then generate an error.

Variable Naming Rules

When naming variables the following rules apply:

  • They must begin with an alphabetic character

  • They cannot contain embedded periods

  • They must be unique within the same scope. There is more on scopes later in this lesson

  • They must be no longer than 255 characters

Variants and Subtypes

VBScript has a single data type called a variant. Variants have the ability to store different types of data. The types of data that a variant can store are referred to as subtypes. The table below describes the subtypes supported by VBScript.

 
SubtypeDescription of Uses for Each Subtype
Byte Integer numbers between 0 to 255
Boolean True and False
Currency Monetary values
Date Date and time
Double Extremely large numbers with decimal points
Empty The value that a variant holds before being used
Error An error number
Integer Large integers between -32,768 and 32,767
Long Extremely large integers (-2,147,483,648 and 2,147,483,647)
Object Objects
Null No valid data
Single Large numbers with decimal points
String Character strings

Assigning Values

You assign a value to a variable by using the following format:

Variable_name = value

The following examples demonstrate assigning values to variables:

Name = "Larry Roof"

HoursWorked = 50

Overtime = True

Scope of Variables

The scope of a variable dictates where it can be used in your script. A variable's scope is determined by where it is declared. If it is declared within a procedure, it is referred to as a procedure-level variable and can only be used within that procedure. If it is declared outside of any procedure, it is a script-level variable and can be used throughout the script.

The example below demonstrates both script-level and procedure-level variables.

<SCRIPT>

  Dim counter

  Sub cmdButton_onClick

    Dim temp

  End Sub

</SCRIPT>

The variable counter is a script-level variable and can be utilized throughout the script. The variable temp exists only within the cmdButton_onClick sub-procedure.

Constants

VBScript does not provide support for constants, such as you find in other programming languages. You can work around this by assigning values to variables that you have defined as shown in the example below. Here, TAX_RATE is our constant.

<SCRIPT>

  Dim TAX_RATE

  TAX_RATE = .06

  Function CalculateTaxes

    CalculateTaxes = CostOfGoods * TAX_RATE

  End Function

</SCRIPT>

Arrays

The VBScript language provides support for arrays. You declare an array using the Dim statement, just as you did with variables:

Dim States(50)

The statement above creates an array with 51 elements. Why 51? Because VBScript arrays are zero-based, meaning that the first array element is indexed 0 and the last is the number specified when declaring the array.

You assign values to the elements of an array just as you would a variable, but with an additional reference (the index) to the element in which it will be stored:

States(5) = "California"

States(6) = "New York"

Arrays can have multiple dimensions-VBScript supports up to 60. Declaring a two dimensional array for storing 51 states and their capitals could be done as follows:

Dim StateInfo(50,1)

To store values into this array you would then reference both dimensions.

StateInfo(18,0) = "Michigan"

StateInfo(18,1) = "Lansing"

VBScript also provides support for arrays whose size may need to change as the script is executing. These arrays are referred to as dynamic arrays. A dynamic array is declared without specifying the number of elements it will contain:

Dim Customers()

The ReDim statement is then used to change the size of the array from within the script:

ReDim Customers(100)

There is no limit to the number of times an array can be re-dimensioned during the execution of a script. To preserve the contents of an array when you are re-dimensioning, use the Preserve keyword:

ReDim Preserve Customers(100)

Have a question about VBScript? The Intranet Journal community can help. Start a thread in the Intranet Journal Discussion Forum.

Previous  Next

Intranet Journal
VBScript Tutorial
Contents
· What is VBScript?
· How to Use this Tutorial

Lesson 1
· Adding VBScript to Web Pages
· The <SCRIPT> Tag
· Non-Supporting Browsers
· Your 1stirst VBScript Exercise

Lesson 2
· Working with Variables
· Declaring Variables
· Scope of Variables
· Constants
· Arrays
· Your 2nd VBScript Exercise

Lesson 3
· Objects and VBScript
· Adding Objects to Web Pages
· Linking VBScript with Objects
· Your 3rd VBScript Exercise

Lesson 4
· Controlling VBScript Routines
· Conditional Statements
· Looping Statements
· Your 4th VBScript Exercise

Lesson 5
· Using VBScript with Forms
· Validating Your Forms
· Your 5th VBScript Exercise

· Summary

OF INTEREST
· Intranet Journal's JavaScript FAQ
· Basic JavaScript by Example

email this page

Tutorials
and more at:
Intranet Journal's Tutorials
Intranet Journal Favorites

Creating a PHP-Based Content Management System

The Spyware Guide

Introduction to Microsoft SharePoint Portal

Intranet Journal
Part of the EarthWeb Network

Managing Editor
Intranet Journal

Tom Dunlap

EarthWeb Home Page
Jupitermedia Home Page

Media Kit




The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers