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.
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.
Variables store input from the user that is being collected via your web page. Please see the below example for a variable.
Name=InputBox(“Please Enter your Name”)
MsgBox “ The Name you entered was “ & Name
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.
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.
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.
True or False
Integers between –32768 and 32767
Long data between –2147483648 and 2147483647
Large Number with decimal points
No valid data
Integers from 0 and 255
Date and time
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”
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.
< script >
< / 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.
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.
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.
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.
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)
|< HTML >
< HEAD >
< TITLE >vbscript example< / TITLE >
< SCRIPT LANGUAGE="VBScript" >
< ! — Add this to instruct non-IE browsers to skip over VBScript modules.
‘ 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"
< /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.
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.