VBScript DataTypes

2
267
Spread the love
  • 23
  • 29
  • 12
  • 23
  •  
  •  
  •  
  •  
  •  
  •  
    87
    Shares

Datatypes in VbScript
VBScript DataTypes used in UFT

Datatypes in VbScript

Data Types in VBScript:

VBScript has only one data type called a Variant. A Variant is a kind of super data type that can contain different kinds of information.So in nutshell the variant is a special data type that can store many different subtypes. Some other thought says that variant is merely a container for several other data types.

A variant data type changes automatically depending on what kind of data the variable is holding. This concept becomes important when we have VBScript code that needs to interact with COM components.(They may be written in VB,C++ or other COM enabled languages).

The downside of variant is that it takes up more spaces (memory) than many of the specified VB data types.

This post will help you to understand the following topics as well:

  1. what is variant data type in vbscript?
  2. is there any Datatype in Vbscript?
  3. data type in vbscript

VBScript is a loosely typed Language:

In Vbscript the coder need not have to declare the data type of a variable.However what kind of data the variable holds still matters to WSH. A loosely typed language does not provide a way to declare type of a variable. This is the reason we don’t see datatypes in VBScript.

In VbScript we have a super type fit for all, universal data type called Variant.Variant is the only data type in VBScript, It is same  data type returned by all functions in VBScript. It is kind of one fits for all.A subtype is a type within the type.

A variant is a type or called super type and remaining all other subtypes are the child type.
In other words a variant is a generic container in which all subtypes resides.

A variant can hold only one of the subtype at a time.The process of changing subtypes are called type conversion or implicit type coercion.

By default the variant subtype will be integer.

The below discussion is very useful while designing automation framework.

Subtype of Variant:

There are two major subtypes of Variant data type:

  • Implicit
  • Explicit

Implicit:

When we change a subtype and a new value gets assigned to a variant variable, in that case an implicit change in the data subtype happens.In this case the VBScript engine checks the new data and determine automatically what subtype is should be. This automatic conversion is called Implicit type conversion.

Why Implicit option is not good always?

When we ask VBScript engine to define and store a data, it may select any of the relevant subtypes to store the data.Like a numeric data 5 can b stored in String,integer,long or byte.
On the other hand,if we want to make sure that the numeric data is stored inside predefined datatype like integer,we need to use CInt() conversion function.This will ensure that the number 5 is stored as Integer not any other data type.

Implicit type Conversion:

This is a technique where variant variable changes its subtype as per engine defined rules automatically.


Dim a
a=InputBox("Enter a Number that will be multiplied by 10")
Msgbox TypeName(a)
If IsNumeric(a) Then
Msgbox TypeName(a)
a=a*10
Msgbox TypeName(a)
Msgbox "Multiplied by 10 gives"&a
Else
Msgbox "Wrong Input"
End If

Here a is changed from String to int to long based on the content.We can test the type value by using TypeName() method.The first TypeName is String as WSH notices that the data is coming from InputDialogBox. The second TypeName is calculated based on the number we provide. The third TypeName is calculated when the resultant number is too long for integer.

Explicit:

The second way to change a subtype to a variant is explicit type conversion.In this process instead of automatic type identification ,the coders themselves takes specific measures to change the subtype. This type of conversions are done with different VBScript syntaxes and conversion functions.
We can also declare a variable implicitly by simply using its name in our script. That is not generally a good practice because we could misspell the variable name in one or more places, causing unexpected results when our script is run. For that reason, the Option Explicit statement is available to require explicit declaration of all variables.

Option Explicit in VBScript:

The Option Explicit statement should be the first statement in our script.Forces explicit declaration of all variables in a script.As in VBScript,the variable declaration is optional ,that signifies that we can declare or we may not declare a variable before using it.Even though this feature makes the VBScript engine very fast but can cause issues if we do spelling mistake.(VBScript may not catch the spelling mistake a error instead it will create a new variable which may create a bug). To overcome this issue VBScript provides a powerful concept called Option Explicit. We need to put it in the first line of the script. It tells the VBScript engine that all variables used in the script are explicitly declared before they are used. It stop spelling mistake of a variable as a new variable.

 


Option Explicit ' Force explicit variable declaration.
Dim MyVar ' Declare variable.
MyInt = 10 ' Undeclared variable generates error.
MyVar = 10 ' Declared variable does not generate error.
 

VBScript Variant Subtypes:

Beyond the simple numeric or string classifications, a Variant can make further distinctions about the specific nature of numeric information. For example, we can have numeric information that represents a date or a time. When used with other date or time data, the result is always expressed as a date or a time. We can also have a rich variety of numeric information ranging in size from Boolean values to huge floating-point numbers. These different categories of information that can be contained in a Variant are called subtypes. Most of the time, we can just put the kind of data we want in a Variant, and the Variant behaves in a way that is most appropriate for the data it contains.
The following table shows subtypes of data that a Variant can contain.

VBScript Subtype Description:

This list is very much useful while working with data types.

Subtype Description Test Function varType() TypeName() return value
Empty A variant is uninitialized. Value is 0 for numeric variables or a zero-length string (“”) for string variables.No Conversion function available. IsEmpty()-returns True/False based on the variable under test is initialized or not. 0(vbEmpty) Empty
Null Variant intentionally contains no valid data..No Conversion function available IsNull()-returns True/False based on the variable under test is having any data or not 1(vbNull) Null
Boolean Contains either True or False. Conversion function -CBool() No Test function 11(vbBoolean) Boolean
Byte Contains integer in the range 0 to 255.
Conversion function-CByte()
IsNumeric()-returns True/False based on the variable under test is numeric or not. 17(vbByte) Byte
Integer Contains integer in the range -32,768 to 32,767.Conversion function-CInt() IsNumeric()-returns True/False based on the variable under test is numeric or not. 3(vbInteger) Integer
Currency -922,337,203,685,477.5808 to 922,337,203,685,477.5807.Conversion function-CCur() IsNumeric()-returns True/False based on the variable under test is numeric or not. 6(vbCurrency) Currency
Long Contains integer in the range -2,147,483,648 to 2,147,483,647.Conversion function-CLng() IsNumeric()-returns True/False based on the variable under test is numeric or not. 2(vbLong) Long
Single Contains a single-precision, floating-point number in the range -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values.Conversion function-CSng() IsNumeric()-returns True/False based on the variable under test is numeric or not. 4(vbSingle) Single
Double Contains a double-precision, floating-point number in the range -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values.Conversion function-CDbl() IsNumeric()-returns True/False based on the variable under test is numeric or not. 5(vbDouble) Double
Date (Time) Contains a number that represents a date between January 1, 100 to December 31, 9999.Conversion function-CDate() IsDate()-returns True/False based on the variable under test is Date or not. 7(vbDate) Date
String Contains a variable-length string that can be up to approximately 2 billion characters in length.Conversion function-CStr() None 8(vbString) String
Object Contains an object.
No Conversion function available
IsObject() 9(vbObject) Object
Error Contains an error number.
No Conversion function available
No Test Function 10(vbError) Error
Variant Super type for all
Conversion function-CVar()
No Test Function 12(vbVariant) Variant
Decimal Contains decimal data.
No Conversion function available
IsNumeric()-returns True/False based on the variable under test is numeric or not. 14(vbDecimal) Decimal
Array Contains multiple data
No Conversion function available
IsArray()-returns True/False based on the variable under test is array or not. 8192(vbArray) Array

We can use conversion functions to convert data from one subtype to another. In addition, the VarType function returns information about how your data is stored within a Variant.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 23
  • 29
  • 12
  • 23
  •  
  •  
  •  
  •  
  •  
  •  
    87
    Shares

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here