Appendix K The Variant Subtypes

The reference material in this appendix is a companion to the detailed explanation of data typesand the VBScript Variant in Chapter 3. However, a brief description of these concepts is as follows .

VBScript is what is known as a weakly typed programming language, which is the opposite of a ³strongly typed ³ language. A weakly typed language does not allow you to declare variableswith specific data types such as String , Date , or Boolean . Instead, in VBScript all variablesare automatically and implicitly assigned a special data type called Variant . The Variant data type is actually many data types in one. VBScript still has the concept of data types suchas String , Date , and Boolean , but these data types are somewhat hidden inside the Variant type.

The mechanism by which the Variant type encompasses many data types in one is called the subtype . A Variant variable can have one of many different subtypes-but only one subtypeat a time. The subtype of a Variant variable can change in one of two ways: implicitly and explicitly.

An implicit change in subtype occurs when a new value is assigned to a Variant variable and VBScript uses some intelligence to examine the new value and automatically decides what the subtype should be. If the new value fits within the bounds of the already assigned subtype, VBScript will not change it. This automatic subtype change process is called implicit type coercion . As a VBScript programmer, it is very important to understand how implicit type coercion works and when and how it occurs.

A programmer can also explicitly force a change in subtype. The primary way of doing this is with one of VBScript's conversion functions . For example, the CLng() function will change the subtype to Long . Similarly, the CStr() function will change it to String . Sometimes it is necessary to use a conversion function to force a subtype change so that a particular operation or function call will work properly.

The Variant data type, the subtypes, and the ins and outs of implicit and explicit type coercion are covered in great detail in Chapter 3. Even an experienced programmer coming from another language would do well to read this chapter. Management of the Variant subtypes in your scripts is a key to your success as a VBScript programmer.

The following table lays out the particulars of all of the Variant subtypes. This is intended as a reference for those times when you're not sure which subtype you might need or when you're not sure if a particular value is too big for a certain subtype.

Following the Variant Subtypes table is a table of the Visual Basic data types. Visual Basic is a strongly typed language. Visual Basic also supports the Variant data type, but many others besides. The list of Visual Basic data types lines up pretty closely with the list of Variant subtypes. The reason we include this information on the Visual Basic data types is that often your VBScript code will be calling components that use specific data types as parameters or return values. When communicating with external components that expose these standard data types, it is important to make sure that your Variant subtype corresponds to the Visual Basic data type required by the external component.

The Variant Subtypes

Subtype Visual Basic Data Type Equivalent Conversion Function to Force theSubtype Test Function (Other than VarType and TypeName) VarType() Function Return Value (with Named Constant Equivalnt) TypeName() Function Return Value

Empty

N/A

N/A

IsEmpty()

0 ( vbEmpty )

Empty

Null

N/A

N/A

IsNull()

1 ( vbNull )

Null

Integer

Integer

CInt()

IsNumeric()

2 ( vbInteger )

Integer

Long

Long

CLng()

IsNumeric()

3 ( vbLong )

Long

Single

Single

CSng()

IsNumeric()

4 ( vbSingle )

Single

Double

Double

CDbl()

IsNumeric()

5 ( vbDouble )

Double

Currency

Currency

CCur()

IsNumeric()

6 ( vbCurrency )

Currency

Date

Date

CDate()

IsDate()

7 ( vbDate )

Date

String

String

CStr()

None

8 ( vbString )

String

Object

Object

N/A

IsObject()

9 ( vbObject )

Object

Error

N/A

*

None

10 ( vbError )

Error

Boolean

Boolean

CBool()

None

11 ( vbBoolean )

Boolean

Variant

Variant

CVar()

None

12 ( vbVariant )

Variant

Decimal

N/A

*

IsNumeric()

14 ( vbDecimal )

Decimal$

Byte

Byte

CByte()

IsNumeric()

17 ( vbByte )

Byte

Array

N/A

N/A

IsArray()

8192 ( vbArray ) #

Array

*Visual Basic supports conversion functions for the Error and Decimal subtypes called CVErr() and CDec() ,respectively. VBScript, however, does not support these conversion functions. See the sidebar later in this section for more information.

$ Because of a bug in VBScript, the TypeName() function does not support the Decimal 1 subtype (although VarType() does). See the sidebar later in this section for more information.

$ This value is actually returned from the VarType() function in combination with the value for Variant (12) . See the section on arrays at the end of this chapter.


The Visual Basic Data Types

Data Storage Required Range of Allowable Values Comments

Byte

1 byte

0 to 255

Often used to store binary data in the form of a ³Byte array ³

Integer

2 bytes

- 32,768 to 32,767

None

Long

4 bytes

- 2,147,483,648 to 2, 147,483,647

The most commonly used numeric data type

Single

4 bytes

Negative values: - 3.402823 E38 to -1.401298E--45 Positive values: 1.401298E--45 to 3.402823E38

For storing IEEE 32-bit single precision floating point numbers (in other words, numbers with decimals)

Double

8 bytes

Negative values:- 1.79769313486232 E308 to 4.94065645841247E--324Positive values:4.94065645841247E--324 to 1.79769313486232E308

For storing IEEE 64-bit double precision floating point numbers; offers greater precision than the Single

Currency

8 bytes

- 922,337,203,685,477.5808 to 922,337,203,685,477.5807

Automatically rounds to four decimal places

Decimal

14 bytes

With no decimal point:+/ -79,228,162,514,264,337,593,543,950,335 With 28 decimal places:+/- 7.9228162514264337593543950335 Smallest nonzero number:+/- 0.0000000000000000000000000001

Can only be stored in a variant; use when maximum floating point accuracy is needed

Boolean

2 bytes

True or False

Only has two possible values; False can also be represented as zero ( ), and True can also be represented as -1 (or, really, any nonzero value); often used as a success/failure return value for functions; also very common for procedure arguments

String

10 bytes + string length

0 to approximately 2 billion characters

Can be used to store any kind of text characters, numbers, or symbols

Date

8 bytes

January 1, 100 to December 31, 9999

When displayed, by default uses the Windows ³Short Date ³ format setting

Object

4 bytes

Any object reference

A generic data type that can hold a ³late bound ³ reference to any COM object

Variant

16 or 22 bytes

Any data within the range of any of the above data types

Takes up 16 bytes when storing numeric data, 22 bytes when storing string data; can have a different ³subtype ³ depending on the type of value stored within it; also takes up a little more space when storing an array; equivalent to the VBScript Variant type




VBScript Programmer's Reference
VBScript Programmers Reference
ISBN: 0470168080
EAN: 2147483647
Year: 2003
Pages: 242

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net