Have you ever gotten an error that told you you had a "type mismatch" and
wondered what that meant? Well I'm going to tell you!
Something that people who are used to other languages are familiar with is the
concept of data or variable types. Well VBScript is a little weird in this
respect. It uses only one type of variable called a variant. This variant type
can contain any type of data and helps make conversion and manipulation easier.
If a variable looks like a string VBScript treats it like a string and if it
looks like a number then it's a number! In order to differentiate between text,
numbers, dates, and other types, VBScript uses what are called variant subtypes
which basically correspond to the types used in most other languages. Hey
don't look at me.... I didn't come up with the concept. I'm just trying to
explain it! ;)
So... if there's really only one data type, what does a type mismatch mean and
why am I writing a sample about data types? Well... a type mismatch occurs when
you try and do something inappropriate like for example divide a number by a
string or pass a string to a function that only processes numbers! You may have
noticed how I tend to name my variables starting with a lower case prefix. This
is one way programmers attempt to keep track of what type of data a certain
variable is supposed to contain in an attempt to not get type mismatches.
This sample will let you enter data and will try to convert it to the different
variant subtypes telling you which conversions are possible and the resulting
values. For reference I'm attaching a table of the different types and the data
ranges they can contain.
True or False
0 to 255
-32,768 to 32,767
-2,147,483,648 to 2,147,483,647
-3.402823E38 to -1.401298E-45 for negative values 1.401298E-45 to 3.402823E38 for positive values
-1.79769313486232E308 to -4.94065645841247E-324 for negative values 4.94065645841247E-324 to 1.79769313486232E308 for positive values
-922,337,203,685,477.5808 to 922,337,203,685,477.5807
January 1, 100 to December 31, 9999, inclusive
Variable-length strings may range in length from 0 to approximately 2 billion characters
Any object reference
Another variable type not listed here because it's a little more complex is
the Array. An array is just an indexed set of other variables and is actually
a topic in itself so I won't go into them here.
Now to really confuse you... variables can also be Empty which means they have
not yet been assigned any value! This is not the same as variables which are
Null! Variables which are Null have been intentionally set to contain no valid
data. And as if that weren't enough you can also have variables which are equal
to Nothing which means they're an object variable, but don't contain an object
(basically a Null object variable)!
Some functions besides the conversion ones which you might find useful when
playing with data types are VarType, TypeName, IsArray, IsDate, IsEmpty, IsNull,
IsNumeric, and IsObject. Give them a try and pretty soon you'll have variable
types down pat!
Now that I've got you really confused just play with the sample and take a look
at the code... it's really not that bad once you get used to it!