Datasheet
Chapter 1: A Quick Introduction to Programming
7
‘ First declare the variable
Dim SomeVariable
‘ Initialize it with a value
SomeVariable = “Hello, World!”
MsgBox SomeVariable
‘ Change the value of the variable to something larger
SomeVariable = “Let’s take up more memory than the previous text”
MsgBox SomeVariable
‘ Change the value again
SomeVariable = “Bye!”
MsgBox SomeVariable
Each time the script engine comes across a variable, the engine assigns it the smallest chunk of memory
it needs. Initially the variable contains nothing at all so needs little space but as you initialize it with the
string
“Hello, World!” the VBScript engine asks the computer for more memory to store the text. But
again it asks for just what it needs and no more. (Memory is a precious thing and not to be wasted.)
Next, when you assign more text to the same variable, the script engine must allocate even more mem-
ory, which it again does automatically. Finally, when you assign the shorter string of text, the script
engine reduces the size of the variable in memory to conserve memory.
One final note about variables: Once you’ve assigned a value to a variable, you don’t have to throw it
away in order to assign something else to the variable as well. Take a look at this example.
Dim SomeVariable
SomeVariable = “Hello”
MsgBox SomeVariable
SomeVariable = SomeVariable & “, World!”
MsgBox SomeVariable
SomeVariable = SomeVariable & “ Goodbye!”
MsgBox SomeVariable
Notice how in this script, you each time keep adding the original value of the variable and adding some
additional text to it. You tell the script engine that this is what you want to do by also using the name of the
SomeVariable variable on the right side of the equals sign, and then concatenating its existing value with
an additional value using the ampersand (
& ) operator. Adding onto the original value works with num-
bers, too (as opposed to numbers in strings) but you have to use the + operator instead of the
& operator.
Dim SomeNumber
SomeNumber = 999
MsgBox SomeNumber
SomeNumber = SomeNumber + 2
MsgBox SomeNumber
SomeNumber = SomeNumber + 999
MsgBox SomeNumber
c01.indd 7c01.indd 7 8/27/07 7:45:19 PM8/27/07 7:45:19 PM