VBScript - Variables
The basics of any programing langauage starts with how to define variables in a program.
For creating and using a variable in vbscript also we need to learn three things i. Variable Naming Rules
ii. Types of variables iii. How to declare a variable and iv. How to put values into a variable. To become a good programmer
we need to learn one more important thing, the variable naming practices accepted industry wide.
Variable Naming Rules
The following rules apply while naming a variable
a. Variable name should start with a letter
b. It can have letters, numbers, underscore(_) etc
c. Variable name cannot have spaces or periods(.) in it.
d. Variable name cannot have more than 255 characters in length.
e. Variable names are NOT case sensitive in vbscript.
These are some examples of variable names: total_amount, average, IntAge etc
Types of Variables
Basically there are two types of variables. Primitive variables and Object variables.
If you want to store an integer value or a string value in a variable (just one value), you
need a primitive variable. But what if you want to store two integer values and a string value
in the same variable; Then you need an Object variable. We will see these in the subsequent sections.
How to declare a variable in a vbscript program
Variables are declared in a vbscript program using the keyword 'Dim' (short for Dimension)
Dim Total_Amount. You are done!! We declared a variable 'Total_Amount'
In vbscript, like other programing languages, we dont need to mention the type of variable.
As soon as you assign a value to a variable, the variable will be created of that type.
The method of declaring a variable is the same for both Primitive variables and Object variables.
Its 'Dim VariableName'
How to put values in a variable? Or How to assign values to a variable?
The method of assigning values to a variable is different for a Primitive variable and an Object variable.
For a Primitive variable we can assign values like the follwoing.
VariableName = Value thats it!! Following are some examples.
Total_Amount = 100 - The variable will be created as integer
Total_Amount = "Hey Whats up man?" - The variable will created as a string variable.
Now for assigning values to an Object variable we need to use a keyword 'Set'. Just like the following.
Set VariableName = Value
Following are some examples.
Set ObjFileSystem = CreateObject(Scripting.FileSystemObject)
Set ObjExcel = CreateObject("Excel.Application")
Variable Naming Practices
To become a good programmer we need to learn on how to name a variable. When you name a variable
You may be able to express two things. i. What kind of value the variable is going to contain and ii. What
value its going to contain. This naming method may not make a difference if you are going to create a
small program and you are going to maintain it in its lifetime. But if you are going to create a program
that is fairly big (more than 500 or 1000 lines) then you will defenitely feel the necessity of this.
Here I am going to list out some naming practices which i follow in my day to day work.
i. Variable Name should start with first 3 or 4 letter of the datatype its going to contain. If its going to
contain an integer value it should start with 'Int', for string variables it should start with 'Str', for Object
variables it should start with 'Obj' etc..
ii. Give the variable a meaning full name. Dont be a miser in the length when you name a variable.
For example you are creating a variable to store the total credit amount of a user. Name the variable as
'IntTotalCreditAmount' not 'totcramt'. Anywhere in the program, by seeing the variable name, you will
understand that this variable contains an integer value and it contains the 'total credit amount'. Thats our goal!!
iii. Always start a variable with an Uppercase letter. All starting letters of each distinct word in the variable name
also should be uppercased. All other letters should be lower case.
eg. 'IntTotalCreditAmount' - Notice the I, T, C and A. I feel this method of naming will make the variable look
beautiful and easy to read.
All these above are just suggestions only; Technically not required for programing :-)