vbscript data types interview questions

VBScript Data Types Interview Questions

1) What is the Only Data Type of VBScript?

VBScript has only one data type called a Variant. A Variant is a special kind of data type that can contain different kinds of information, depending on how it is used. Because Variant is the only data type in VBScript, it is also the data type returned by all functions in VBScript.

At its simplest, a Variant can contain either numeric or string information. A Variant behaves as a number when you use it in a numeric context and as a string when you use it in a string context. That is, if you are working with data that looks like numbers, VBScript assumes that it is numbers and does what is most appropriate for numbers. Similarly, if you're working with data that can only be string data, VBScript treats it as string data. You can always make numbers behave as strings by enclosing them in quotation marks (" ").

2) When Data Conversion is required?

If we read data then VBScript considers that data as String type data, in order to perform mathematical operations we need to convert the Data using conversions functions.

3) Give an example for Data conversion?

Converting data from one sub type to another:

Ex:
Dim num1, num2
num1=InputBox("Enter num1 Value")
num2=InputBox("Enter num2 Value")

num1 = Cint(num1)
num2 = Cint(num2)

Msgbox num1+num2
----------------------------------
Dim num1, num2
num1=Cint(InputBox("Enter num1 Value"))
num2=Cint(InputBox("Enter num2 Value"))

Msgbox num1+num2
------------------------------------------
Dim num1, num2
num1=InputBox("Enter num1 Value")
num2=InputBox("Enter num2 Value")

Msgbox VarType(num1) '8 for string
Msgbox VarType(num2) '8 for string

num1 = Cint(num1)
num2 = Cint(num2)

Msgbox VarType(num1)
Msgbox VarType(num2)

Msgbox num1+num2
--------------------------------------------
Dim Tickets
Tickets = Window("Flight Reservation").WinEdit("Tickets:").GetVisibleText()
Msgbox VarType(Tickets) '8

Tickets = Cint(Tickets)
Msgbox VarType(Tickets) '2
--------------------------------------------
Dim val
val = InputBox ("Enter a value")
Msgbox VarType(val) '8

val= Cdbl(val)
Msgbox VarType(val)
-------------------------------------
Dim Price
Price = Window("Flight Reservation").WinEdit("Price:").GetVisibleText()
Msgbox VarType(Price) '8

Price = Cdbl(Price)
Msgbox VarType(Price) '5

4) How to check Data Sub Types in VBScript?

 
Using VarType Function we can check Data sub types, VarType Function Returns Constant based result, ex: 2 for integer, 8 for string etc...

Example:
----------
Get Data sub types using VarType Function
-----------------------------------------
Dim a
Msgbox VarType(a) '0 for Uninitialized / Empty

a="India"
Msgbox VarType(a) '8 for string

a=100
Msgbox VarType(a) ' 2 for Integer

a="100"
Msgbox VarType(a) '8 for string

Msgbox VarType("India") '8 for string

a=10.45
Msgbox VarType(a) '5 for Double

a=#10/10/2012#
Msgbox VarType(a) '7 for Date

Set a = CreateObject("Scripting.FileSystemObject")

Msgbox VarType(a) '9 for Automation Object

5) What are the subtypes of data that a Variant has?

The following are the subtypes of data that a Variant can contain:
EmptyVariant : Uninitialized. Value is 0 for numeric variables or a zero-length string ("") for string variables.
NullVariant: Iintentionally contains no valid data.
Boolean : Contains either True or False
Byte : Contains integer in the range 0 to 255.
Integer : Contains integer in the range -32,768 to 32,767.
Currency : -922,337,203,685,477.5808 to 922,337,203,685,477.5807.
Long : Contains integer in the range -2,147,483,648 to 2,147,483,647.
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.
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.
Date : Contains a number that represents a date between January 1, 100 to December 31, 9999
Time : Represents a time between 0:00:00 and 23:59:59
String : Contains a variable-length string that can be up to approximately 2 billion characters in length.
Object : Contains an object.
Error : Contains an error number.

6) How to Declare Data Types in VBScript?

VB Script doesn't support explicit declaration of data types, in VB Script only data type is Variant.

0 comments:

Post a Comment