VBScript Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.01

Variable Inspection and Numeric Conversion

Part:   1   2  3 

VB Script Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Data Types and Literals

Variables

Logic Operations

String Operations

Conditional Statements

Arrays

Loop Statements

Functions and Subroutines

Built-in Functions

Variable Inspection

... Table of Contents

This chapter describes:

  • Variable Inspection
  • Variable Inspection Example
  • Parsing Long Integers

Notes and samples in this chapter are based Visual Basic 6.0.

Variable Inspection

If are writing a function, and receiving a variable from the calling code, you can not always assume that the variable is of certain type and has value assigned. If you code your function with such assumptions, your code will crash if the calling code failed to meet those assumptions.

To better protect your code, you want to inspect the variable 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 like: object, array, or single data.

2. Determine the data type of the variable like, integer, long, double, date, Boolean, or String.

3. Determine the status of the data like, empty or not empty.

VB offers a number of built-in functions to help you to inspect a variable:

  • 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
  • IsObject(variable) - Returns True if the specified variable is an object
  • TypeName(variable) - Returns the type name of the specified variable

Variable Inspection Example

To show you how those inspection function work, I wrote the following example, variant_subtype_detection.html:

<html>
<body>
<!-- variable_inspection.html
   Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<pre>
<script language="vbscript">
   document.writeln(" ")
   document.writeln("Checking variants with value:")
   document.writeln("V1 " & GetVarInfo(777) & " (777)")
   document.writeln("V2 " & GetVarInfo(777777) & " (777777)")
   document.writeln("V3 " & GetVarInfo(3.14) & " (3.14)")
   document.writeln("V4 " & GetVarInfo(3.3e200) & " (3.3e200)")
   document.writeln("V5 " & GetVarInfo("Hello") & " (""Hello"")")
   document.writeln("V6 " & GetVarInfo(TRUE) & " (TRUE)")
   document.writeln("V7 " & GetVarInfo(#31-Dec-1999#) _
      & " (#31-Dec-1999#)")

   document.writeln(" ")
   document.writeln("Checking strings with value:")
   document.writeln("S1 " & GetVarInfo("777") & " (""777"")")
   document.writeln("S2 " & GetVarInfo("3.14") & " (""3.14"")")
   document.writeln("S3 " & GetVarInfo("True") & " (""True"")")
   document.writeln("S4 " & GetVarInfo("#31-Dec-1999#") _
      & " (""#31-Dec-1999#"")")

(Continued on next part...)

Part:   1   2  3 

Dr. Herong Yang, updated in 2006
VBScript Tutorials - Herong's Tutorial Notes - Variable Inspection and Numeric Conversion