VBScript Variables: How to Declare and Use Variables – VBScript Dim

Introduction to VBScript Variables: Tutorial #2

In my previous tutorial on “Introduction to VBScript“, we discussed some of the introductory topics of VBScript.

In this tutorial, we will discuss on one of the basic but most important topics of VBScript that will help you to move towards the next level thereby gaining knowledge of working on the VBScript code. It is very important to know about the usage of VBScript Variables before proceeding in any programming language.

This tutorial will enable you to learn about the scope of Variables and how Variables are declared and used in VBScript.

Declaring Variables in VBScript

Variables are like Containers where the values can be stored and changed as well during the Script Execution.

These are named as memory locations which can be accessed by giving some names i.e. we can refer any variable by its name to see what value it contains or if we need to change any value. Variable is the key aspect of learning any language easily and efficiently.

 

Rules for Naming Variables/Naming Conventions

There are certain points to be kept in mind, rather we can say that there are certain rules to be followed before naming a variable.

They are:

  • Variable Name must always start with an alphabet. E.g: output, name, etc. And Variable Name should not start with a number or any special character like _va123, 12non, etc.
  • Variable Name cannot exceed a limit of 255 characters
  • Variable Name should never contain a period (.)

These rules should be strictly followed by a good programming practice.

Now, it is important to know how variables are declared.

Variable Declaration & Its Types

Any variable must be declared first, before using. In various languages, different keywords are used to declare a variable.

Let’s see how a variable is declared in the VBScript.

It is a good programming practice to declare the variable first and then starts using it in order to avoid any problems/confusions arising in the remaining code. I will brief on this point in a while.

We can declare variables using Public, Private or Dim Statements.

Example:

If you want to declare a variable ‘age’ then it can be declared as follows:

Dim age

You can declare multiple variables in a single line by separating it with Comma (,) as shown below:

Dim employee, name, school, class

In this way, a variable can be created.

Types of Variable Declaration:

There are 2 ways in which a variable can be declared. They are as given below.

#1) Implicit Declaration

When variables are used directly without declaration, it is termed as Implicit Declaration. However, it’s not a good practice because if at times a variable name is not spelled correctly in the script then it can produce weird results while running and sometimes, it will not be easy as well to detect this by the user.

Let’s understand this with a simple Example:

age = 10

In this case, if you misspell “age” variable for “aeg”, the script will automatically create a new variable with the name ‘aeg’.

To overcome this, you can use ‘Option Explicit’, which is discussed below.

#2) Explicit Declaration

Declaring variables before using them is called as Explicit Declaration of variables. This is what we have done above.

Eg: Dim age

So, before moving to the next topic, let me explain a bit about Option Explicit.

Option Explicit: 

This provides a mechanism where the user has to declare all the variables using Dim, Public or Private Statements before using them in the Script.

In option explicit, if a user tries to use the variables which are not declared in case of Option Explicit then an error occurs. It is always recommended to use ‘Option Explicit’ at the top of the code so that even if unintentionally you used a wrong name of the variable then you can correct it immediately without any confusion.

Assigning Values to Variables in VBScript

Assigning values to the variables in VBScript is done in the same manner as in other programming languages. After the declaration of a variable, a value is assigned to it with the help of an Equal (=) Operator.

Name of the Variable comes on the left and the value which is assigned to the Variable is on the Right Hand Side of the ‘=’ Operator.

Let’s understand this with few simple Examples:

Example 1:

If a variable to which a value is to be assigned is of a String type then it can be assigned using double quotes (“”).

<html>
<head>
<title>Let’s learn assigning values to variables</title>
</head>
<body>
<script type=”text/vbscript”>
Dim msg
msg = “Hello Everyone”
Msgbox msg ‘this will show Hello Everyone message in the message box
</script>
</body>
</html>

Example 2:

If a variable to which a value is to be assigned is of a Numeric type then it can be assigned without using double quotes.

<html>
<head>
<title>Let’s learn assigning values to variables</title>
</head>
<body>
<script type=”text/vbscript”>
Dim val
val = 10
Msgbox val ‘this will show 10 in the message box
</script>
</body>
</html>

Now, let’s understand the scope of the variables.

Scope of the Variables

The scope of the Variables provides an easy to see the visibility of a variable to the point to which it can be accessible. Let’s discuss it in detail in order to understand in a better way.

A variable can be declared by using any of the 3 keywords – Dim, Public or Private.

The question which arises now is “what is the difference in their usage and when is a particular one used?” I will try to explain each of these keywords with the help of examples.

Dim If a variable is declared using Dim keyword inside the function then its scope will be limited to the function level only i.e. this variable cannot be accessed once the function ends.

Following is an Example for Dim:

<html>
<head>
<title>Let’s learn about scope of the variables</title>
</head>
<body>
<script type=”text/vbscript”>
Dim val1
Call output()
Function output()
 val1 = 11
 Dim val2
 val2 = 12
 Dim val3
 val3 = (val1 + val2) * 10
 document.write(val1) ‘this will produce 11 as output
 document.write(val2) ‘this will produce 12 as output
 document.write(val3) ‘this will produce 230 as output
End Function
Msgbox val1 ‘this will show 11 as val1 is declared at script level using Dim Msgbox val2 ‘this will not show anything as val2 is declared inside the function
Msgbox val3 ‘this will not show anything as val3 is declared inside the function </script>
</body>
</html>

Dim Keyword

PrivateIf a variable is declared using Private keyword inside the function then its scope will not be limited to function level alone, but it can be accessed everywhere in that particular script. Here, the scope of a variable is wider than in the case of Dim as it’s not limited to the function level but to a particular script.

Following is an Example for Private:    

<html>
<head>
<title>Let’s learn about scope of the variables</title>
</head>
<body>
<script type=”text/vbscript”>
Dim val1
Call output()
Function output()
 val1 = 11
 Private val2
 val2 = 12
 Private val3
 val3 = (val1 + val2) * 10
 document.write(val1) ‘this will produce 11 as output
 document.write(val2) ‘this will produce 12 as output
 document.write(val3) ‘this will produce 230 as output
End Function
Msgbox val1 ‘this will show 11 as val1 is declared at script level using Dim Msgbox val2 ‘this will show 12 as val2 is declared Private inside the function
Msgbox val3 ‘this will show val3 as val3is declared Private inside the function 
</script>
</body>
</html>

Private Keyword

Public If a variable is declared using Public keyword inside the function then its scope will not be limited till function level, but it can be accessed everywhere in that particular script and in other scripts too. Here, the scope of a variable is much wider than that in case of Dim and Private.

Following is an Example for Public:

<html> 
<head>
<title>Let’s learn about scope of the variables</title>
</head>
<body>
<script type=”text/vbscript”>
Dim val1
Call output()
Function output()
 val1 = 11
 Public val2
 val2 = 12
 Public val3
 val3 = (val1 + val2) * 10
 document.write(val1) ‘this will produce 11 as output
 document.write(val2) ‘this will produce 12 as output
 document.write(val3) ‘this will produce 230 as output
End Function
Msgbox val1 ‘this will show 11 as val1 is declared at script level using Dim Msgbox val2 ‘this will show 12 as val2 is declared Public inside the function
Msgbox val3 ‘this will show val3 as val3is declared Public inside the function 
</script>
</body>
</html>

Public Keyword

Let’s move ahead towards the important topic “Declaring and Assigning Values to Arrays”.

Declaring & Assigning Values to Arrays

Arrays itself is a very important topic to understand. Hence will discuss Arrays in detail in one of my coming tutorials but right now I just wish to show you the declaration of array variables.

The need of an Array arises when you want to have a variable which can carry a series of values in one memory location.

Following is the procedure to declare an Array:

Dim Arraycount (4) – This means that this is an array having 5 values starting from index ‘0’.

To assign values to this array, you can give any numeric or string type values for each index. Use double quotes for assigning string values.

arraycount (0) = 1
arraycount (1) = 2
arraycount (2) = 3
arraycount (3) = 4
arraycount (4) = 5

Multidimensional Array: When there is more than one array then it is known as a Multidimensional array. Let’s assume that there is an array of 2 dimensions i.e. there are 6 rows and 6 columns, hence it can be declared as follows:

Dim arrayage (6, 6)

Conclusion

This was all about Variables, their usage, and scope in VBScript Programming Language.

I hope that this tutorial will be easier for you to proceed further with other subsequent topics of this series.

Next Tutorial #3: We will discuss more on Constants, Operators and Operator precedence in the next tutorial.

Stay tuned and Feel free to share your thoughts, also let us know if you have any doubts about this tutorial.