Saturday 18 May 2013

Data Types in vbscript

In vb script all variables are initialized with the data type called variant.
But we can have below sub-types in vb script.

In below table we have mentioned all sub data types in vb script. In next post we will see how to use these sub data types in vb script examples.


Subtype
Description
Empty
Variable is uninitialized. Value is 0 or ""
Null
Variable has Invalid data.
Boolean
True/False
Byte
Contains integer (0 to 255).
Integer
Contains integer (-32,768 to 32,767).
Currency
-922,337,203,685,477.5808 to 922,337,203,685,477.5807.
Long
Contains integer (-2,147,483,648 to 2,147,483,647).
Single
Contains a single-precision, floating-point number
Double
Contains a double-precision, floating-point number
Date (Time)
Contains a date
String
Contains a String
Object
Contains an object.
Error
Contains an error number.


Examples -

In below example we have a variable x and we have assigned a value 10 to x. In the next statement 
when we use typename method to check the data type of variable x then it will show it as a integer.

x = 10
MsgBox TypeName(x) 'prints integer
x = 11.1

MsgBox TypeName(x) 'prints double

If we store the value 11.1 in the same variable x, then it's data type will be shown as Double. So this is how data type of the variables keep changing in the VBScript. So VBScript language is called as loosely typed language. And the mechanism to determine the data type of variables at runtime is called as late binding.

Data Type Conversion
We can also convert the data type of the variable explicitly using various data type conversion functions.
For example - In below code we have a VBScript which will convert the decimal number into integer.
a = 11.3

Msgbox TypeName(a) 'prints double

a = cint(a)
Msgbox Typename(a) 'prints Integer

Other data type conversion functions are.
  1. cdbl - converts to double
  2. cstr - converts to string
  3. cbool - converts to boolean
  4. cbyte - converts to byte
  5. ccur - converts to currency
  6. cdate - converts to date
  7. clng - converts to long integer





No comments:

Post a Comment

Total Pageviews