variables i think everyone knows what a variable is, but to be safe, a variable is a little chunk of memory in which a value of varying type (like characters and numbers) can be stored and manipulated. that is not the most precise or technical definition that could be used, but i suspect everyone gets the picture. variables are what you use to store the different values floating around in your program. as i hinted at rather blatantly above, variables can take on many different forms. they can be strings (a string is one or more characters — like a sentence for example), integer, floating point number, a date, and all sorts of other useful things.
one advantage to a scripting language is that it is not strongly typed. or, in other words, to use a variable it does not need to be declared with a type defined. whereas in a strongly-typed language like c++, you need to declare not only the variable but what type of variable it is.
in vbscript there is really only one type, variant. therefore, all vbscript variables are of type variant (which means they can basically be anything) and may be any of the following sub-types:
subtype description empty variant is uninitialized. value is 0 for numeric variables or a zero-length string ("") for string variables. null variant intentionally contains no valid data. boolean contains either true or false. byte contains integer in the range 0 to 255. integer contains integer in the range -32,768 to 32,767. currency -922,337,203,685,477.5808 to 922,337,203,685,477.5807. long contains integer in the range -2,147,483,648 to 2,147,483,647. single contains a single-precision, floating-point number in the range -3.402823e38 to -1.401298e-45 for negative values; 1.401298e-45 to 3.402823e38 for positive values. double contains a double-precision, floating-point number in the range -1.79769313486232e308 to -4.94065645841247e- 324 for negative values; 4.94065645841247e-324 to 1.79769313486232e308 for positive values. date (time) contains a number that represents a date between january 1, 100 to december 31, 9999. string contains a variable-length string that can be up to approximately 2 billion characters in length. object contains an object. error contains an error number. source: microsoft scripting technologies
the neat thing about the variant thing is that you do not really need to worry about type at all. it will figure out what sub-type the variable is, based on how you use it. what is important to recognize is that if you utilize a variable as one sub-type (say as an integer) and then try to store a string in it later, that will return an error because of a mismatch.
to use a variable in code, it first has to be declared — or memory space has to be set aside for it. vbscript uses the "dim" statement to declare variables (there are others but we do not need to worry about those right now). here is an example of using the dim statement.
<% ' this little single apostrophe is a comment, ' it allows you to put explanation in your ' code and not have the interpreter run it!
' use this as notes to yourself for what code ' does or notes for future developers working ' on your page!
'using the dim statement
dim myvariable dim anothervariable, x, y, count %>
the above code is very simplistic. the first dim statement sets up a variable called myvariable. the second line declares anothervariable, x and y all at basically the same time. it is there to simply demonstrate that you can declare more then one variable per line, as long as you separate each variable with a comma.
now before you get all excited and start declaring variables until your heart is content, you should know that there are a few restrictions for naming variables. they must begin with an alphabetic character (i.e. they cannot start with a number), they cannot contain an embedded period (ie. no my.variable type naming is acceptable), they cannot exceed 255 characters, and they must be uniquely named (i.e. cannot have two different variables named jim in the same function or sub).