Variables and Constants
What is a Variable?
· Variable is a virtual Container/Placeholder where you store a desired information, called value, in the computer’s memory.
· To see or change its value, you refer it by its name.
· In VBScript, variables are always of one fundamental data type, Variant.
Types of Variable
- · Scalar Variable
- · Array Variables
Difference between Scalar variable and Array variable
The beauty of an array is that it enables you to store and use a series of data using one variable name and an index to distinguish the individual items. By using an index, you can often make your code simpler and more efficient so that it’s easier for you to put a script together and change it later. With VBScript, the elements inside your array can hold any kind of data. The elements don’t have to be all integers or all strings. Thus, an array can hold a combination of data types.
Naming Standards of a Var:
- MUST always begin with an alpha character
- MUST be a unique name in the scope in which it is declared
- MUST NOT exceed 255 characters
- MUST NOT have a period a character
- MUST NOT be the name of a keyword already used in VBScript.
Example 1: alpha, alpha123, alpha_123
Example 2: 123alpha, alpha.123, alpha.alpha, Date, Time,
Overriding Naming Standards
You can override standard variable naming conventions by placing your variable name in brackets. This allows you to use reserved words and/or illegal characters in variable names.
Dim [1st_name], [Date]
[1st_name] = “Jojo”
[Date] = “October 2, 1962″
Assigning value to a variable
The variable name is to the left followed by an equality sign followed by the value.
<unique name> = <value>
Agent_Name = “jojo”
Num_Tickets = 3
• Explicit Declaration of a variable
Dim statement variable if declared at the script level are available to all procedures within the script. At the procedure level, variables are available only within the procedure.
Public statement variables are available to all procedures in all scripts.
Private statement variables are available only to the script in which they are declared.
Dim oUser_ID, oPassword
oUser_ID = “jojo”
oPassword = “mercury”
• Implicit Declaration of a variable
You can also declare a variable implicitly by simply using its name in your script.
User_ID = “jojo”
Note: This is not a good practice, as you can make a fat-finger mistake.
UserID = “Jojo”
Option Explicit Statement
• The Option Explicit statement forces explicit declaration of all variables in a script.
• The Option Explicit statement should be the first statement in your script.
User_ID = “jojo”
In situations where you have several variables, it can get very messy to declare and use the variables. A good example will be a grocery list. When you have several items, say around 50, it is difficult to assign each one to a variable. Instead, you must put all the variables, i.e. elements, in a super variable called Array. The count of item in an Array starts with 0.
Dim <Name of Array> (number of items in the array)
<Name of Array>(0) = <value_1>
<Name of Array>(1) = <value_2>
<Name of Array>(nth item) = <value_n>
myArray(0) = “Milk”
myArray(1) = “Bread”
myArray(2) = “Eggs”
myArray(3) = “Cookies”
This code would display “Bread”
Please note “myArray(3)” indicates that myArray has 4 elements.
Thus, a single variable, myArray, can assume four different values.
What are constants?
A constant is a variable within a program that never changes in value. Users can create their own constants by initializing variables accordingly and then not changing their value.
Constants perform a similar function to variables. Like variable they are a symbolic replacement for an item of data in memory. The difference is that a constant keeps the same value throughout its lifetime.
Values are assigned to constants using the same method used for variables, and can contain the same range of data subtypes. The accepted method of denoting a constant is to use all capitals for the name. In this case, the use of underscores improves their readability and is highly recommended.
You create user-defined constants in VBScript using the Const statement. Using the Const statement, you can create string or numeric constants with meaningful names and assign them literal values.
Const TEMPERATURE = 72
Const DEPART_DATE = “12/12/2007”
Const FIRST_NAME = “Jojo”
VBScript pre-defined constants
A number of useful constants you can use in your code are built into VBScript. Constants provide a convenient way to use specific values without actually having to remember the value itself. Using constants also makes your code more maintainable should the value of any constant ever change. Because these constants are already defined in VBScript, you don’t need to explicitly declare them in your code. Simply use them in place of the values they represent.
- Color Constants: Defines eight basic colors that can be used in scripting.
- Date and Time Constants: Defines date and time constants used by various date and time functions.
- Date Format Constants: Defines constants used to format dates and times.
- Miscellaneous Constants: Defines constants that don’t conveniently fit into any other category.
- MsgBox Constants: Defines constants used in the MsgBox function to describe button visibility, labeling, behavior, and return values.
- String Constants: Defines a variety of non-printable characters used in string manipulation.
- Tristate Constants: Defines constants used with functions that format numbers.
- VarType Constants: Defines the various Variant subtypes.
Please refer to QuickTest Professional Help for a complete list of the constant values.