VB Script – Working with Variables
In this tutorial you will learn about VB Script – Wroking with variables, Introduction, Description, Naming Conventions, Variants and its sub types, How to Assign values to a variable ? What is the scope of a variable ? Constants, Arrays and Conclusion.
Introduction
A variable is a named location in your computer memory which can be used for data storage during the working of your vb scripts or any other language.Variables plays a vital role to store values . Variables can be defined further into two categories.
One is local variable and second are global variables. Local variables values will be only accessible in the part of the function in which that variable has been defined whereas global variable will be accessible in the whole program.
Description
Variables store input from the user that is being collected via your web page. Please see the below example for a variable.
Sub cmdClickMe_onClick
Name=InputBox(“Please Enter your Name”)
MsgBox “ The Name you entered was “ & Name
End Sub
Now, in the above example the second line shows a variable with the name of “Name”. Here one Input box will be displayed to the user with a message Please enter your name and what ever entered by the user will be stored in a variable called Name and later on displayed to the user with the help of MsbBox function.
There is something left in the variables i.e How to declare a variable. There are two types in which variable is declared one is explicitly and one is implicitly. Well the preferred method is explicitly with the help of dim statement.
For ex. Dim age
Here we have declared variable with the name of age . In this way you can also declare multiple variable in a single line only.
For ex. Dim name,age,status,birthdate
The above given examples are of explicit declaration of the variables. For implicit declaration you can declare and use the variable at any time within your code.But the main disadvantage of implicit declaration is that it is very hard to debug the code when the error occurs. So in my opinion always prefer the variables to declare explicitly. There are some naming conventions attached when you declare a variable.
Naming Conventions
Whenever you want to declare a variable always follow below naming conventions.
- The variables always begin with an alphabet instead of any number.
- They should not contain spaces.
- They must be unique in their names.
- If you want to define a long name than always concatenate the names with and underscore.
Ex. Fname_Lname
Always start your variable name with capital alphabet letter and when the second word starts in the variable make it capital too.
They must not be larger than 255 characters in length.
Variants and its sub types
VbScript deals with a single datatype called variant. Datatype means the kind of the data a variable will store.It can be a character data,string data,number data , byte data and a Boolean data. But you don’t have to worry in Vb Script as it only has a datatype of variant to store any type of data.
Sub Type |
Description |
Boolean |
True or False |
Integer |
Integers between –32768 and 32767 |
Long |
Long data between –2147483648 and 2147483647 |
String |
Character Strings |
Single |
Large Number with decimal points |
Null |
No valid data |
Currency |
Monetary values |
Byte |
Integers from 0 and 255 |
Date |
Date and time |
Double |
Large numbers with decimal points |
How to Assign values to a variable ?
Simple you have to declare a variable name and assign any value.
For ex. Name = “Chandra”
Status=False
Age=30
Now all the above variables has been assigned values.This is a simple way to declare and assign related values to a variable.
What is the scope of a variable ?
The scope of a variable defines whether a variable will be accessible in the whole function or will be accessed only to its local instance.I have defined earlier in the tutorials that they can also be deemed as a local variables or can be deemed as a global variables.
For ex.
< script >
Dim name
Sub cmdclickme_OnClick
Dim age
End Sub
< / script >
It is clear from the above example about the scope of the variable that the variable name will be available to the whole script as it is declared outside sub procedure so enhance his behaviour to the global as compared to the variable name age which is defined inside the sub procedure hence making his behaviour local and will be only accessed in this sub procedure only.
Constants
Vb Scripts does not support constants. Constants are the values which are not changed throughout the program as you might have read in other programming languages.
Arrays
If you have done any programming language you might have heard about the Arrays and VbScript positively supports arrays. You can declare any array with the help of dim statement.
For ex. Dim country(20)
The above array has been created with the 20 elements, it means you can easily store 20 elements and will be referred by a single name. These type of arrays are sometimes called fixed arrays.Arrays index always start from zero. As given in the above example we have declared an array of 20 elements but when you put the values or retrieve the values from the array you have to start from the index 0 to index 19.To store values in an array just follow the simple strategy.
country(0)=”India”
country(1)=”US”
country(2)=”Canada”
Arrays too have a multi dimensions . To declare a multi dimensional array example is given below.Suppose you want to store a student name as well his/her age.To store the values in an array you have to reference both dimensions.
Dim nameage(30,0)=”Rahul”
Dim nameage(30,1)=”Mehta”
Dim nameage(30,2)=”Chandra”
As in the above example we know how many values will be stored in an array.But sometimes we don’t know how many values will be stored in an array than there arises a need for dynamic arrays.To declare a dynamic array see the below example.
Ex. Dim users()
In this way we can declare a dynamic array without initializing its size.But to change the size you can use the below given code.
Ex. Redim users(100)
In this way you can assign the size of the array. Now whenever you want to increase the size of the array again you must preserve the previous values you have stored in an array.To preserve the previous values use.
Redim Preserve users(100)
.
.
Example :
< HTML > < HEAD > < TITLE >vbscript example< / TITLE > < SCRIPT LANGUAGE="VBScript" > < ! — Add this to instruct non-IE browsers to skip over VBScript modules. Option Explicit Sub cmdCalculate_OnClick Dim AmountofTax Dim CRLF Dim Message Dim Subtotal Dim TABSPACE Dim TAX_RATE Dim TotalCost ‘ Define our constant values. TAX_RATE = 0.06 CRLF = Chr(13) & Chr(10) TABSPACE = Chr(9) ‘ Perform order calculations. Subtotal = Document.myform.txtQuantity.Value * Document.myform.txtUnitPrice.Value AmountofTax = Subtotal * TAX_RATE TotalCost = Subtotal + AmountofTax ‘ Display the results. Message = "The total for your order is:" Message = Message & CRLF & CRLF Message = Message & "Subtotal:" & TABSPACE & "$" & Subtotal & CRLF Message = Message & "Tax:" & TABSPACE & "$" & AmountofTax & CRLF Message = Message & "Total:" & TABSPACE & "$" & TotalCost MsgBox Message,,"Your Total" End Sub –> < /SCRIPT > < /HEAD > < BODY > < FORM NAME="myform" > < TABLE > < TR > < TD >< B >Quantity:< /B >< /TD > < TD >< INPUT TYPE="Text" NAME="txtQuantity" SIZE=5 >< /TD > < /TR > < TR > < TD >< B >Unit price:< /B >< /TD > < TD >< INPUT TYPE="Text" NAME="txtUnitPrice" SIZE=5 >< /TD > < /TR > < /TABLE > < BR > < INPUT TYPE="Button" NAME="cmdCalculate" VALUE="Calculate Cost" > < /FORM > < /BODY > < /HTML > |
The output on the browser would be as under :
The commonly used operands are addition,subtraction, multiplication and subtraction. The result of calculations are stored in a variable called subtotal.All the calculations are than displayed by using the MsgBox function. The ampersand character is used to concatenate two strings.
Conclusion :
Till now you must have learn the types of variables the vbscript uses. How to declare variables and use it within the script and what a comment line in a script.