Vbscript data types
-
Upload
online-it-training -
Category
Education
-
view
187 -
download
0
description
Transcript of Vbscript data types
VBSCRIPT introduction:
Microsoft Visual Basic Scripting Edition brings active scripting to a wide variety of environments, including Web client scripting in Microsoft Internet Explorer and Web server scripting in Microsoft Internet Information Service.
Easy to Use and Learn
If you already know Visual Basic or Visual Basic for Applications (VBA), VBScript will be very familiar. Even if you do not know Visual Basic, once you learn VBScript, you are on your way to programming with the whole family of Visual Basic languages.
Windows Script VBScript talks to host applications using Windows Script. With Windows
Script, browsers and other host applications do not require special integration code for each scripting component.
Adding VBScript Code to an HTML Page You can use the SCRIPT element to add VBScript code to an HTML page.
The <SCRIPT> Tag VBScript code is written within paired <SCRIPT> tags.
<SCRIPT LANGUAGE="VBScript"> <!– Function CanDeliver(Dt) CanDeliver = (CDate(Dt) - Now()) > 2 --> End Function </SCRIPT>
<HTML> <HEAD> <TITLE>Test Button Events</TITLE> </HEAD> <BODY> <FORM NAME="Form1"> <INPUT TYPE="Button" NAME="Button1" VALUE="Click"> <SCRIPT FOR="Button1" EVENT="onClick" LANGUAGE="VBScript"> MsgBox "Button Pressed!" </SCRIPT>
</FORM> </BODY> </HTML>
VBScript Data Types 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.
You 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.
conversion functionsAsc FunctionReturns the ANSI character code corresponding to the first letter in a string.
Dim MyNumber MyNumber = Asc("A") ' Returns 65. MyNumber = Asc("a") ' Returns 97. MyNumber = Asc("Apple") ' Returns 65.
CBool FunctionReturns an expression that has been converted to a Variant of subtype Boolean. Dim A, B, Check A = 5: B = 5 ' Initialize variables. Check = CBool(A = B) ' Check contains True. Msgbox Check A = 0 ' Define variable. Check = CBool(A) ' Check contains False. Msgbox Check
CByte Function Returns an expression that has been converted to a Variant of subtype Byte.
Dim MyDouble, MyByte MyDouble = 125.5678 ' MyDouble is a Double. MyByte = CByte(MyDouble) ' MyByte contains 126.
CCur Function Returns an expression that has been converted to a Variant of subtype Currency. Dim MyDouble, MyCurr MyDouble = 543.214588 ' MyDouble is a Double. MyCurr = CCur(MyDouble * 2) ' Convert result of MyDouble * 2 (1086.429176) to a Currency
(1086.4292).
CDate Function Returns an expression that has been converted to a Variant of subtype Date. MyDate = "October 19, 1962" ' Define date. MyShortDate = CDate(MyDate) ' Convert to Date data type. MyTime = "4:35:47 PM" ' Define time. MyShortTime = CDate(MyTime) ' Convert to Date data type.
CDbl FunctionReturns an expression that has been converted to a Variant of subtype Double.
Dim MyCurr, MyDouble
MyCurr = CCur(234.456784) ' MyCurr is a Currency (234.4567). MyDouble = CDbl(MyCurr * 8.2 * 0.01) ' Convert result to a Double (19.2254576).
Chr Function Returns the character associated with the specified ANSI character code. Dim MyChar MyChar = Chr(65) ' Returns A. MyChar = Chr(97) ' Returns a. MyChar = Chr(62) ' Returns >. MyChar = Chr(37) ' Returns %.
CInt Function Returns an expression that has been converted to a Variant of subtype Integer. Dim MyDouble, MyInt MyDouble = 2345.5678 ' MyDouble is a Double. MyInt = CInt(MyDouble) ' MyInt contains 2346.
CLng Function Returns an expression that has been converted to a Variant of subtype Long. Dim MyVal1, MyVal2, MyLong1, MyLong2 MyVal1 = 25427.45: MyVal2 = 25427.55 ' MyVal1, MyVal2 are Doubles. MyLong1 = CLng(MyVal1) ' MyLong1 contains 25427. MyLong2 = CLng(MyVal2) ' MyLong2 contains 25428.
CSng Function Returns an expression that has been converted to a Variant of subtype Single.
Dim MyDouble1, MyDouble2, MySingle1, MySingle2 ' MyDouble1, MyDouble2 are Doubles. MyDouble1 = 75.3421115: MyDouble2 = 75.3421555 MySingle1 = CSng(MyDouble1) ' MySingle1 contains 75.34211. MySingle2 = CSng(MyDouble2) ' MySingle2 contains 75.34216.CStr Function Returns an expression that has been converted to a Variant of subtype String. Dim MyDouble, MyString MyDouble = 437.324 ' MyDouble is a Double. MyString = CStr(MyDouble) ' MyString contains "437.324".
Hex FunctionReturns a string representing the hexadecimal value of a number.
Dim MyHex MyHex = Hex(5) ' Returns 5. MyHex = Hex(10) ' Returns A. MyHex = Hex(459) ' Returns 1CB.
OctReturns a string representing the octal value of a number.
Dim MyOct MyOct = Oct(4) ' Returns 4. MyOct = Oct(8) ' Returns 10. MyOct = Oct(459) ' Returns 713.