VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

GetVarInfo() - Variable Inspection Example

This section provides a tutorial example of writing a code, GetVarInfo(), to print out data type information of a given variable.

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

<html>
<body>
<!-- variable_inspection.html
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<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#"")")

   document.writeln(" ")
   document.writeln("Checking arrays:")
   Dim aFixed(5)
   document.writeln("A1 " & GetVarInfo(aFixed) & " (Fixed)")
   aFixed(5) = 555
   document.writeln("A2 " & GetVarInfo(aFixed) & " (Fixed/Val.)")

   Dim aDynamic()
   document.writeln("A3 " & GetVarInfo(aDynamic) & " (Dyn.)")
   ReDim aDynamic(3)
   document.writeln("A4 " & GetVarInfo(aDynamic) & " (Dyn./Set)")
   aDynamic(3) = "Dog"
   document.writeln("A5 " & GetVarInfo(aDynamic) & " (Dyn./Set/Val.)")

   document.writeln(" ")
   document.writeln("Checking emptiness:")
   vNull = Null
   document.writeln("E1 " & GetVarInfo(vNull) & " (Null)")
   vEmpty = Empty
   document.writeln("E2 " & GetVarInfo(vEmpty) & " (Empty)")

   document.writeln(" ")
   document.writeln("Checking objects:")
   document.writeln("O1 " & GetVarInfo(Document) & " (Document)")

Function GetVarInfo(vAnyThing)
   If IsObject(vAnyThing) Then
      sObject = "Obj=Y"
   Else
      sObject = "Obj=N"
   End If

   If IsArray(vAnyThing) Then
      sArray = "Array=Y"
   Else
      sArray = "Array=N"
   End If

   If IsDate(vAnyThing) Then
      sDate = "Date=Y"
   Else
      sDate = "Date=N"
   End If

   If IsNumeric(vAnyThing) Then
      sNumeric = "Num=Y"
   Else
      sNumeric = "Num=N"
   End If

   If IsNull(vAnyThing) Then
      sNull = "Null=Y"
   Else
      sNull = "Null=N"
   End If

   If IsEmpty(vAnyThing) Then
      sEmpty = "Empty=Y"
   Else
      sEmpty = "Empty=N"
   End If

   sType = TypeName(vAnyThing)
   GetVarInfo = sObject & " " & sArray & " " & sDate _
      & " " & sNumeric & " " & sNull & " " & sEmpty & " " & sType
End Function
</script>
</pre>
</body>
</html>

Here is the output:

 
Checking variants with value:
V1 Obj=N Array=N Date=N Num=Y Null=N Empty=N Integer (777)
V2 Obj=N Array=N Date=N Num=Y Null=N Empty=N Long (777777)
V3 Obj=N Array=N Date=N Num=Y Null=N Empty=N Double (3.14)
V4 Obj=N Array=N Date=N Num=Y Null=N Empty=N Double (3.3e200)
V5 Obj=N Array=N Date=N Num=N Null=N Empty=N String ("Hello")
V6 Obj=N Array=N Date=N Num=Y Null=N Empty=N Boolean (TRUE)
V7 Obj=N Array=N Date=Y Num=N Null=N Empty=N Date (#31-Dec-1999#)
 
Checking strings with value:
S1 Obj=N Array=N Date=N Num=Y Null=N Empty=N String ("777")
S2 Obj=N Array=N Date=Y Num=Y Null=N Empty=N String ("3.14")
S3 Obj=N Array=N Date=N Num=N Null=N Empty=N String ("True")
S4 Obj=N Array=N Date=N Num=N Null=N Empty=N String ("#31-Dec-1999#")
 
Checking arrays:
A1 Obj=N Array=Y Date=N Num=N Null=N Empty=N Variant() (Fixed)
A2 Obj=N Array=Y Date=N Num=N Null=N Empty=N Variant() (Fixed/Val.)
A3 Obj=N Array=Y Date=N Num=N Null=N Empty=N Variant() (Dyn.)
A4 Obj=N Array=Y Date=N Num=N Null=N Empty=N Variant() (Dyn./Set)
A5 Obj=N Array=Y Date=N Num=N Null=N Empty=N Variant() (Dyn./Set/Val.)
 
Checking emptiness:
E1 Obj=N Array=N Date=N Num=N Null=Y Empty=N Null (Null)
E2 Obj=N Array=N Date=N Num=Y Null=N Empty=Y Empty (Empty)
 
Checking objects:
O1 Obj=Y Array=N Date=N Num=N Null=N Empty=N HTMLDocument (Document)

There are a number of interesting notes here:

  • Line V3 shows that (3.14) is reported as (Double). I was expecting Single.
  • Line V6 shows that (TRUE) is reported as (Num=Y). What number TRUE will be converted to?
  • Line S1 shows that ("777") is reported as (String) and (Num=Y). That is reasonable.
  • Line S2 shows that ("3.14") is reported as (String), (Num=Y) and (Date=Y). What date "3.14" will be converted to?
  • Line A3 shows that a dynamic-size array is not Empty even before calling ReDim to set its size.
  • Lines A1 to A5 show that arrays will never be Empty or Null.
  • Lines E1 to E2 show that Empty and Null are independent data types and data literals.
  • Line E2 shows that Empty is reported as (Num=Y). What number Empty will be converted to?
  • Line O1 shows that (document) is an object of "HTMLDocument" type. This object is created by IE and passed the IE VB script environment.

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

 Arithmetic Operations

 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

 Built-in Functions

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)

 References

 Printable Copy - PDF Version

GetVarInfo() - Variable Inspection Example - Updated in 2015, by Dr. Herong Yang