Variable Inspection - Data Type Validation
This section describes why variables need to be inspected when receiving it in a procedure and how to inspect a variable to determine its data type.
If are writing a procedure, and receiving a variable from the calling code through an argument,
you can not always assume that value of the variable is of certain subtype.
If you code your procedure with some assumptions, your code will crash if the calling code
fails to meet those assumptions.
To better protect your code, you want to inspect the value first, and write your code
based on the result of the inspection. Variable inspection can be performed at 3 different levels:
1. Determine the structure type of the variable. Is it an object, array, or scalar.
2. Determine the data subtype of the variable. If it is an object, check its properties;
If it is an array, check its elements; If it is a scalar, check the subtype of the value:
Byte, Integer, Long, Single, Double, Date, Currency, Boolean, or String.
3. Determine the status of the data. Is it Empty or Null.
VBScript offers a number of built-in functions to help you to inspect a variable:
- IsObject(variable) - Returns True if the specified variable is an object
- IsArray(variable) - Returns True if the specified variable is an array
- IsDate(variable) - Returns True if the specified variable can be converted to a date
- IsEmpty(variable) - Returns True if the specified variable is Empty
- IsNull(variable) - Returns True is the specified variable is Null
- IsNumeric(variable) - Returns True if the specified variable can be converted to a number
- TypeName(variable) - Returns the type name of the specified variable
- SubType(variable) - Returns the subtype code of the specified variable
Table of Contents
About This Book
Introduction of VBScript - Visual Basic Scripting Edition
Variant Data Type, Subtypes, and Literals
Numeric Comparison Operations and Logical Operations
String Operations - Concatenation and Comparison
Variable Declaration and Assignment Statement
Expression and Order of Operation Precedence
Statement Syntax and Statement Types
Array Data Type and Related Statements
Array References and Array Assignment Statements
Conditional Statements - "If ... Then" and "Select Case"
Loop Statements - "For", "While", and "Do"
"Function" and "Sub" Procedures
►Inspecting Variables Received in Procedures
►Variable Inspection - Data Type Validation
GetVarInfo() - Variable Inspection Example
GetInteger() - Crash-Free Integer Conversion
Error Handling Flag and the "Err" Object
Regular Expression Pattern Match and Replacement
scrrun.dll - Scripting Runtime DLL Library
Creating Your Own Classes
IE Web Browser Supporting VBScript
IIS ASP Server Supporting VBScript
WSH (Windows Script Host)
PDF Printing Version