VBScript - The Easy Stuff - Page 2
February 5, 2001
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).
Back to Basics: VBScript for ASP
Back to Basics: VBScript for ASP
More Easy Stuff - Page 3
|