Type of Variables
We all know that when we use variables within a program, we must define the type of variable; if the variable is used with integers, you declare the variable as an integer. But defining each variable can result to problems in code and also create a complex structure that increases the overhead required for programs.
To avoid this, VBScript automatically allocates a variable type to any variable you define in your programs. Variable types used by VBScript include: Boolean, Byte, Integer, Long, Single, Double, Date, String, Object, Error, Empty, and Null.
Generally, if you use whole numbers, such as 5 or 8, with a variable, VBScript creates the variable as an integer. Variables with values that use decimal points, such as 3.5 or 5.82, are usually assigned as double-precision floating-point values-Doubles. Variables entered with a mixture of alphabetical and numeric characters, such as H2O or 4-H, are created as Strings.
Because VBScript can automatically convert between some variable types, most variable conflicts are eliminated. However, if you try to add a String variable to a numeric variable type, you will get an error; so if a variable has alphanumeric characters, don't try to perform numeric calculations with it.
Note
Before performing calculations, you can make sure variables are numeric by using the IsNumeric function. This function returns a value of true if the variable is numeric and false if it isn't. This sample code checks for a numeric variable:
If IsNumeric(Value1) = True Then
Value2 = Value1 + 5
These related functions check other variable types: IsDate, IsEmpty, IsNull, IsNumeric, and IsObject.
Quick Recap
In today’s lesson, we learned a bit about the VBScript Language, its pros and cons too. We’ve also learned how to insert our VBScripts within our HTML code by placing the VBScripts within the <SCRIPT LANGUAGE = “VBScript”> and </SCRIPTS> tags. We followed the begin script tag with a begin comment tag, and precede the end script tag with an end comment tag to hide VBscripts to non-compatible browsers.
I also taught you how to declare variables explicitly by using the Dim keyword followed by the variable name (e.g. Dim score) or implicitly by just assigning a value to a variable without first declaring it (e.g. score = 4). And finally we discovered that we VBScript automatically allocate a variable type to any variable you define in our programs.
Hope that this first tutorial on VBScripts has been helpful to you. Next time, we’ll learn on VBScripts’ arrays and on arithmetic and comparison operators. Your comments and questions are the most welcome.