Variables in VB

Started by thiruvasagamani, Aug 12, 2008, 09:10 PM

Previous topic - Next topic

thiruvasagamani

What is a Variable?

A variable is a "container" for information you want to store. A variable's value can change during the script. You can refer to a variable by name to see its value or to change its value. In VBScript, all variables are of type variant, that can store different types of data.

Rules for Variable Names:

Must begin with a letter
Cannot contain a period (.)
Cannot exceed 255 characters
Declaring Variables

You can declare variables with the Dim, Public or the Private statement. Like this:  dim name
name=some value


Now you have created a variable. The name of the variable is "name".

You can also declare variables by using its name in your script. Like this:name=some value


Now you have also created a variable. The name of the variable is "name".

However, the last method is not a good practice, because you can misspell the variable name later in your script, and that can cause strange results when your script is running. This is because when you misspell for example the "name" variable to "nime" the script will automatically create a new variable called "nime".  To prevent your script from doing this you can use the Option Explicit statement. When you use this statement you will have to declare all your variables with the dim, public or private statement. Put the Option Explicit statement on the top of your script. Like this:

option explicit
dim name
name=some value
Thiruvasakamani Karnan