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

Data Type "Variant()" - Array of Variant Values

This section describes the array data type, Variant(). A tutorial example is provided to show how TypeName() and VarType() functions work on array values.

Earlier, we learned that VBScript supports only one data type: Variant, The above statement needs to be revised now because of arrays.

VBScript supports only one data type, Variant, for scalar values. Array is another data type supported in VBScript for non-scalar values.

A non-scalar value is a complex value that contains an internal structure to store more than a single piece of data.

An array is non-scalar value that contains an array structure to store a list of scalar values.

Since there is only one data type, Variant, for scalar values, VBScript only supports one array type: Array of Variant, or Variant()

Applying data type rules to variables, now we know:

  • A variable can be declared as a scalar variable using "Dim x".
  • A variable can be declared as an array variable using "Dim y()".
  • A scalar variable can be used to store a scalar value of data type, Variant.
  • An array variable can be used to store an array of scalar values of data type, Variant.
  • A scalar variable has a data type of Variant.
  • An array variable has a data type of Variant() - Array of Variant.

To show you the difference between a scalar variable and an array variable, I wrote the following example, array_type.html:

<html>
<body>
<!-- array_type.html
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<pre>
<script language="vbscript">
   Dim aScalar
   Dim anArray(1)
   
   aScalar = "Rabbit"
   anArray(0) = "Dog"
   anArray(1) = "Cat"
   
   document.writeln("")
   document.writeln("The scalar variable:")
   document.writeln("   IsArray(aScalar): " & IsArray(aScalar))
   document.writeln("   TypeName(aScalar): " & TypeName(aScalar))
   document.writeln("   (VarType(aScalar)=vbString): " _ 
      & (VarType(aScalar)=vbString))
   
   document.writeln("")
   document.writeln("The array variable:")
   document.writeln("   IsArray(anArray): " & IsArray(anArray))
   document.writeln("   TypeName(anArray): " & TypeName(anArray))
   document.writeln("   (VarType(anArray)=vbArray+vbVariant): " _ 
      & (VarType(anArray)=vbArray+vbVariant))
   document.writeln("   IsArray(anArray(0)): " & IsArray(anArray(0)))
   document.writeln("   TypeName(anArray(0)): " _ 
      & TypeName(anArray(0)))
</script>
</pre>
</body>
</html>

Here is the output:


The scalar variable:
   IsArray(aScalar): False
   TypeName(aScalar): String
   (VarType(aScalar)=vbString): True

The array variable:
   IsArray(anArray): True
   TypeName(anArray): Variant()
   (VarType(anArray)=vbArray+vbVariant): True
   IsArray(anArray(0)): False
   TypeName(anArray(0)): String

The output shows that:

  • For a scalar value, the "TypeName()" function returns the subtype name of the value.
  • For a scalar value, the "VarType()" function returns the subtype code of the value.
  • For an array value, the "TypeName()" function returns "Variant()".
  • For an array value, the "VarType()" function returns the sum of two codes: "vbArray+vbVariant".

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

 What Is an Array?

 "Dim x()" - Declaring Array Variables

 "x(i)" - Accessing Array Elements with Indexes

 "Dim x(n)" - Fixed-Size Array Example

 "Dim x()" - Dynamic-Size Array Example

 "For Each" Statement Example

 "Erase" Statement - Removing All Elements in an Array

Data Type "Variant()" - Array of Variant Values

 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

 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

Data Type "Variant()" - Array of Variant Values - Updated in 2015, by Dr. Herong Yang