Introduction To VB Script What Is A Variable?
Introduction To VB Script What Is A Variable?
What is a variable?
A variable is a virtual container in the computer's memory or
convenient placeholder that refers to a computer memory location
where you can store program information that may change during
the time your script is running.
Where the variable is stored in computer memory is unimportant.
What is important is that you only have to refer to a variable by
name to see or change its value. In VBScript, variables are always of
one fundamental data type, Variant.
A computer program can store information in a variable and then
access that information later by referring to the variable's name.
The first way, called the explicit method, is where you use the Dim
keyword to tell VBScript you are about to create a variable. You then
follow this keyword with the name of the variable. If, for example,
you want to create a variable called Quantity, you would enter
Dim nQuantity
And the variable will then exist.
Syntax
Dim varname[([subscripts])][, varname[([subscripts])]] . . .
Tip
Use Option Explicit to avoid incorrectly typing the name of an existing
variable or to avoid confusion in code where the scope of the variable
is not clear.
The following example illustrates use of the Option Explicit
statement.
Dynamic Arrays
The second type of array you can create is the dynamic array. The benefit of
a dynamic array is that if you don't know how large the array will be when
you write the code, you can create code that sets or changes the size while
the VBScript code is running.
A dynamic array is created in the same way as a fixed array, but you don't
put any bounds in the declaration. As a result, your statement becomes
Dim arrNames()
Eventually, you need to tell VBScript how many elements the array will
contain.
You can do this with the ReDim function. ReDim tells VBScript to "redimension the array to however many elements you specify. ReDim takes
dimensions the same way Dim can. The syntax is
ReDim arrName(nCount - 1)
VBScript Constants
Sometimes in writing code, you will want to refer to values that never
change.
Users can create their own constants by initializing variables
accordingly and then not changing their value.
Using the Const statement, you can create string or numeric
constants with meaningful names and assign them literal values. For
example:
Conditional Statements
Using conditional statements, you can write
VBScript code that makes decisions and
repeats actions
If...Then...Else statement
If value = 0 Then
MsgBox value
ElseIf value = 1 Then
MsgBox value
ElseIf value = 2 then
Msgbox value
Else
Msgbox "Value out of range!"
End If
VBScript Procedures
Kinds of Procedures
In VBScript there are two kinds of
procedures; the Sub procedure and the
Function procedure.
Sub Procedures
A Sub procedure is a series of VBScript
statements, enclosed by the Sub and End Sub
statements, that performs actions but doesn't
return a value. A Sub procedure can take
arguments (constants, variables, or expressions
that are passed by a calling procedure). If a Sub
procedure has no arguments, its Sub statement
must include an empty set of parentheses.
Function Procedures
A Function procedure is a series of VBScript statements
enclosed by the Function and End Function statements. A
Function procedure is similar to a Sub procedure, but can
also return a value. A Function procedure can take
arguments (constants, variables, or expressions that are
passed to it by a calling procedure). If a Function procedure
has no arguments, its Function statement must include an
empty set of parentheses. A Function returns a value by
assigning a value to its name in one or more statements of
the procedure. The return type of a Function is always a
Variant.
FileSystemObject Basics
MsgBox Function
Description
Displays a dialog box containing a message,
buttons, and optional icon to the user.
Syntax
MsgBox(prompt, buttons, title, helpfile, context)
Instr Functions
Description
Returns the position of the first occurrence of one string within
another.
Syntax
InStr (start, string1, string2, compare)
Arguments
Return Value
Variant of type Long.
Notes
The return value of InStr is influenced by the values of string1 (string to
search) and string2 (string to find)
If the start argument is omitted, InStr commences the search with the first
character of string1.
If the start argument is Null, an error occurs.
Example
Arguments
Return Value
Variant of type String.
Notes
LCase affects only uppercase letters; all other characters in string are
unaffected.
LCase returns Null if string contains a Null.
Left Function
Description
The Left function returns a specified number of characters
from the left side of a string.
Syntax
Arguments
Notes
If length is 0, a zero-length string (" ") is returned.
If length is greater than the length of string, string is returned.
If length is less than 0 or Null, the function generates runtime error
If string contains Null, Left returns Null.
Right Function
Description
The Right function returns a specified number of characters from
the right side of a string.
Syntax
Arguments
Return Value?
Notes
Len Function
Description
The Len function returns the number of characters in a string or the
number of bytes required to store a variable.
Syntax
Arguments
Return Value
Notes
string and varname are mutually exclusive; that is, you must specify either
string or varname, but not both.
If either string or varname is Null, Len and LenB return Null.
You can't use Len with an object variable.
If varname is an array, you must also specify a valid subscript. In other
words, Len can't determine the total number of elements in or the total size
of an array.
Arguments
Return Value
Notes
If string contains a Null, LTrim returns Null.
If string contains a Null, RTrim returns Null.
If string contains a Null, Trim returns Null.
UBound Function
Description
The UBound returns the largest available subscript for the indicated
dimension of an array.
Syntax
Arguments
LBound Function
Description
The LBound function returns the smallest available subscript for the
indicated dimension of an array.
Syntax
Arguments
Notes
If dimension isn't specified, 1 is assumed. To determine the lower
limit of the first dimension of an array, set dimension to 1, to 2 for
the second, and so on.
Split Function
Description
The Split function returns a zero-based, one-dimensional array
containing a specified number of substrings.
Syntax
Arguments
Return Value
A variant array consisting of the arguments passed into the function.
Notes
If delimiter isn't found in expression, Split returns the entire string
in element 0 of the return array.
If delimiter is omitted, a space character is used as the delimiter.
If count is omitted or its value is -1, all strings are returned.
The default comparison method is vbBinaryCompare. If delimiter is
an alphabetic character, this setting controls whether the search for it
in expression is case-sensitive (vbBinaryCompare) or not
(vbTextCompare).
Array Function
Description
The Array function returns a variant array containing the elements
whose values are passed to the function as arguments.
Syntax
Arguments
Return Value
Notes
Although the array you create with the Array function is a variant
array data type, the individual elements of the array can be a mixture
of different data types.
The initial size of the array you create is the number of arguments
you place in the argument list and pass to the Array function.