VBScript Tutorial – Table of Content
In this VBScript Tutorial, we are going to learn VBScript Variables, VBScript Array, VBScript Constants, and Dictionary Object in VBScript.
VBScript Tutorial #1: VBScript Variables
VBScript is the shorter form of Visual Basic Scripting which follows the Visual Basic language. The VBScripting was introduced by Microsoft. It’s a very simple languages to learn which allows to develop server-side as well as client-side scriptings.
· It is a simple scripting language.
· It is case insensitive and easy to learn for any entry-level programmer.
· It is an object-based programming language and does not follow the OOPS concept.
· It supports the COM which is a shorter form of Component Object Model (COM) structure. It allows to manage the elements from the environment where it is getting executed.
· The VBScripting which executed in server side, is compatible with hosting platform such as Internet Explorer web browser, Internet Information Services (IIS) as web server and Windows Scripting Host (WSH).
· It can be used to develop both server-side(Classic ASP) and client-side scripting.
· Tools like Excel macro, UFT (Unified Functional Testing) follow the VB Scripting.
VBScript as a client-side scripting:
VBScript as a window scripting:
Disadvantages of VBScript:
· VB Scripts are supported in internet explorer only. The web browsers which are well known in market like Mozila Firefox, google Chrome etc., are not compatible with VBScript.
· It is platform-dependent i.e., only supported by windows environment.
· The debugging of VBScript is challenging as there is non availability of proper development area.
The purpose of the VBScript comment is to educate the compiler to skip the particular line or lines of code from the execution. Basically, VBScript comments are used to document or write the descriptions/objectives during the scripting. It will help to increase the readability of the code block by writing the description, change the log using the commenting. Two types of VBScript comments are available –
Single line VBScript comment: It’s done by using the single quote ( ‘ ) at the beginning of each line. Example of single-line comment in VBScript –
‘ Single line comment example
Multiple lines VBScript comment: Multiple lines VBScript comment is used to comment on more than one line. This type of VBScript comment can be achieved by putting more lines between “/*” and “*/” symbols. Example of multiple lines VBScript comment –
VB Statement 1
VB Statement 2
….. and so on
The VBScript variables are one kind of placeholders which refer memory locations to store data. During the execution, VBScript allows to modify the variables. The declared name of VBScript variables are used to access the variables for reading or writing purposes. The variables have the ability to interact with any data types such as string, char, number, date, etc. As per the data types, the vbscript variables are changed accordingly. For an example – if we assign a number within double quote, it will be treated as string value.
We can declare VBScript variables in three ways which are explained below –
- VBScript dim
- VBScript public
- VBScript private
Dim stands for declare immediate. It means, VBScript dim allows to declare a variable immediately for any kinds of data types. We can declare multiple variables which are separated by comma, with a single VBScript dim statement. The scope of which are declared using VBScript dim statement, are restricted with in the code block such as functions, sub-procedures, local scripts etc., where it was declared. Below examples has shown the syntax of VBScript dim statement for variable declaration – ,
Dim var1, var2, var3
Using the keyword dim, we can declare the variables immediately with specifying the logical name of the variables. It is mandatory to declare the each of the variables if we specifying the statement “Option Explicit” at the starting of the script. So, if the statement does not specified, the variable declaration is an optional step. In this case, the variables are auto declared while defining the variables.
When variables are declared with the keyword public, the variables are accessible from through out the entire script. Basically, public keyword is used to define a variable as global variable.
When variables are declared with the keyword private, the scope of the variables are restricted to with in the code block.
Assigning Values to VBScript Variables:
In the below example, we will declare (using VBScript dim satement) assign values such as number and string to the variables with the help of equal(=) symbol,
'''''declare variables using VBScript dim statement dim numVar dim strVar '''''define the variables - number and string numbVar = 12345667788 strVar = "This is sample variable"
While assigning values to the variable, we need to keep the variable name in the left side of equal symbol and value has to be kept in the right side. For strings or characters, we need to keep the value with in double quote.
Scalar Variables – Defining a variable with single value, is known as scalar variable.
Array Variables – Conceptually an array represents a list of values. In VBScript, when a variable is assigned with multiple same type of values, is know as an array. Array can be declared with parenthesis after the variable name. It can be declared in same way how we declare a variable. The structure to declare an array is shown below –
Dim myArrayName(max index)
Here the index is used to refer the each elements of an array which start from zero. So, the max index denotes the length of array minus one. Values can be assigned to the each array element using the array indexes. In the below example, we will define an array which holds 3 student names –
' Declare an array with VBScript dim statement Dim arrStudentName(2) ' Assigning the student names using the index arrStudentName(0) = "Tom" arrStudentName(1) = "Jack" arrStudentName(2) = "Jerry"
VBScript Variables – Naming Convention:
Syntactically, there is no specific naming conventions are available. But it advisable to follow the below guidelines for better readability of scripts,
· The starting character of a variable should be alphabetic.
· Any embedded period should not be kept.
The name of the variable should be logical as per the usage.
Length should not cross the limit of 255 characters.
The value of VBScript constant variable can not be modified after definition. So, the constant variable is basically a read-only variable. It has to defined during the declaration. The VBScript provides default constant variable which can be used during scripts. The example of default constants are – vbOK, vbCancel, vbTrue, vbFalse, etc. The Const keyord is used to declare a constant in VBScripting. Below example, we will see how to define constant variables for number and strings –
Const strConstant= “This is my string.”
Const numConstant = 123
Conceptually an array represents a list of values. In VBScript, when a variable is assigned with multiple same type of values such as string, integer, etc., is know as an array. The VBScripts allows to keep the list of same type values in the same memory location(VBScript array). The each array elements can be accessed using the reference numbers which is known as array index. The index is always starts from zero.
Structure wise, VBScript Array has two sections – the logical name of array name and the array subscript that denotes the highest index value. The array subscript is equals to the length of array minus one.
The VBScript array has to be declared first, otherwise it will throw errors. Similar to the variable declaration, we can declare the VBScript array using the dim statement. By considering usage or scope, VBScript two type of arrays –
· Local VBScript Array – The scope of this type arrays are restricted to the code block (functions or sub procedures) where it was declared.
· Global VBScript Array – We can use this type of arrays throughout the scripts. This type of arrays should be declare at the beginning of the script.
Static and Dynamic Arrays:
Also, we can declare the VBScript array as static or dynamic while creating the scripts.
A static VBScript array – It has a fixed number of array elements which can not be altered during the script execution.
A dynamic VBScript array – The number of array element can be changed at anytime. When the number of elements are not fixed or pre-defined, we can use this array.
Working with VBScript Arrays:
Syntax for array declaration – Dim myArray(subscript value)
In the below example, we will create an VBScript array of marks of 3 students and store the calculated average marks into a variable –
'Declaration of array and value assignment for three students dim arrMarks(2) arrMarks(0) = 10 arrMarks(1) = 15 arrMarks(2) = 20 'Calculate the average and store it in a variable Dim avgMarks avgMarks = (arrMarks(0)+arrMarks(1)+arrMarks(2))/3
Dictionary Object in VBScript:
The dictionary object in VBScripts, has the similarity with the VBScript array. Which means, it’s also allowing to store a list of values. But the primary differences of dictionary object in VBScript, are that it can holds different types of data and a key has to be assigned for each data instead of indexes.
The dictionary object in VBScript can be declared for usages with the reference of “Scripting.Dictionary” class.
Advantages of Dictionary Object in VBScript:
· Different types of data can be stored in a single variable.
· With the help of key, we can access the corresponding elements easily.
· This concept is very flexibles as there is predefined methods are available to manipulate the dictionary object in vbscript.
Example – Dictionary Object in vbscript:
In the below example, we will see the codes for different operations on dictionary objects –
' **** Create a Dictionary Object in VBScript **** 'Create a variable. Dim dict Set dict = CreateObject("Scripting.Dictionary") 'Adding keys and items. dict.Add "Name", "VBScript" dict.Add "Id", "1" dict.Add "Trainer", "K Mondal" '**** Checking the availability of a Key from the dictionary object in VBScript **** If dict.Exists("Name") Then msg = "The name key exists" Else msg = "The name key does not exists" End If 'Read an item dim sName sName = dict.Item("Name") 'get the count of element dim nCount nCount = dict.count 'Delete single item dict.Remove("Name") 'Delete All item dict.RemoveAll
In this VBScript article, we have learned about the Overview of VBScript Variables, VBscript Array, VBScript Constants, and Dictionary Object in VBScript. For more details on VBScript, please click here.