当前位置:文档之家› VBScript脚本语言讲义

VBScript脚本语言讲义

. TypeName及VarType函数介绍,示例程序如下:可用VarType 函数来返回某个数据的Variant 子类型,如下面的代码:Dim strInput,strVarType,strTypeNamestrInput="hello world"MsgBox strInputstrVarType=VarType(strInput)MsgBox "VarType:"&strVarTypestrTypeName=TypeName(strInput)MsgBox "TypeName:"&strTypeName问题:如下三段语句,结果分别是什么?Dim VarTypeCheckVarTypeCheck = VarType(300)MsgBox VarTypeCheckVarTypeCheck = VarType(#10/19/62#)MsgBox VarTypeCheckVarTypeCheck = TypeName("VBScript")MsgBox VarTypeCheck. Option Explicit 声明,如果变量没有定义就使用要报错:Option ExplicitDim MyDateMyDate = "October 19, 1962"If IsDate(MyDate) thenMyShortDate = CDate(MyDate)End ifMsgBox MyShortDate变量的作用域与存活期:Dim strMainCall ChangeValueSub ChangeValue()Dim strSubstrMain="hello world!"MsgBox "strMain InSub:"&strMainstrSub="hello liuxiaolin"MsgBox "strSub InSub:"&strSubEnd SubMsgBox "strMain in Main:"&strMainMsgBox "strSub in Main:"&strSub数组的定义:Dim 数组名(n)实际上数据会有n+1个元素,下标从0到nDim MyArray(10),iFor i=0 To 10MyArray(i)=iMsgBox MyArray(i)NextMsgBox "min is "&LBound(MyArray) '返回数组的最小可用下标MsgBox "max is "&UBound(MyArray) '返回数组的最大可用上标利用Redim重新定义数据的大小,加上preserve关键字保存原来数组的内容:Dim MyFamily()ReDim MyFamily(1)MyFamily(0)="0"MyFamily(1)="1"ReDim MyFamily(2).'ReDim Preserve MyFamily(2)MyFamily(2)="2"Dim iFor i=0 To 2MsgBox MyFamily(i)Next在字符串中回车换行的方法:chr(13)&chr(10) / vbCr&vbLf / vbCrLf / vbNewLineDim str,str1,str2,str3,str4str="how are you fine,thank you!"str1="how are you"&chr(13)&chr(10)&"fine,thank you!"str2="how are you"&vbCr&vbLf&"fine,thank you!"str3="how are you"&vbCrLf&"fine,thank you!"str4="how are you"&vbNewLine&"fine,thank you!"MsgBox strMsgBox "str1:"&str1MsgBox "str2:"&str2MsgBox "str3:"&str3MsgBox "str4:"&str4Chr用法:Dim strstr=chr(34) & "Hello" & chr(34)MsgBox str.Const常量Const a=1a=2IF…THEN…ELSEIF…ELSE…END IF语句:Dim House,CarHouse=NullCar=nullIf IsNull(House) Or IsNull(Car) ThenMsgBox "现在我们还不成熟,还是再等等吧!"ElseMsgBox "OK,我嫁给你!"End If课堂练习:找出3个整数中的最大数并输出,三个整数存在三个变量intA,intB,intC中.Dim intA,intB,intC,maxintA=8intB=5intC=3If intA>=ntB Thenmax=intAelsemax= intBEnd IfIf max>=intC ThenMsgBox maxElseMsgBox intCEnd IfDim intA,intB,intC,tempintA=CInt(InputBox("请输入整数A:"))intB=CInt(InputBox("请输入整数B:"))intC=CInt(InputBox("请输入整数C:"))If intA>=intB Thentemp =intAelsetemp =intBEnd IfIf temp >=intC ThenMsgBox tempElseMsgBox intCEnd IfSelect…case…case else…End Select语句:Dim strstr=InputBox("请输入你要说的话!")Select Case strCase "hello"MsgBox "hello"Case "how are you"MsgBox "fine,thank you "Case ElseMsgBox "thanks"End Select.Case后接的表达式可以是任意字符,如:case 1,也可以是多个表达式,如:case 5,6,7,但是vbs中不支持给出case后的范围的格式课堂练习:输入一个字符,判断字符类型:大写、小写、数字、其他。

给出相应的提示信息。

Option ExplicitDim strValuestrValue = InputBox ("请输入一个字符:")strValue = CInt(Asc(strValue))MsgBox strValueIf strValue>=65 And strValue<=90 ThenstrValue=1ElseIf strValue>=97 And strValue<=122 ThenstrValue=2ElseIf strValue>=48 And strValue<=57 ThenstrValue=3End ifSelect Case strValueCase 1MsgBox "您输入的是大写字母!"Case 2MsgBox "您输入的是小写字母!"Case 3MsgBox "您输入的是数字!"Case ElseMsgBox "您输入的是特殊字符"End SelectDo…Loop循环语句的使用:推荐使用while循环'即使不符合条件也会做一次.Dim intAgeintAge=0DointAge=intAge+1MsgBox CStr(intAge)Loop While intAge<=5'不符合条件时,则一次也不做intAge=0Do While intAge<=5intAge=intAge+1MsgBox CStr(intAge)Loop'达到条件时就不再进入循环了。

而while语句在达到条件时也要再进入循环一次Dim intAgeintAge=0Do Until intAge=5intAge=intAge+1MsgBox intAgeLoopintAge=0DointAge=intAge+1MsgBox intAgeLoop Until intAge=5'Do循环支持Exit Do语句Dim intAgeintAge=0Do Until intAge=5intAge=intAge+1MsgBox intAgeIf intAge=3 ThenExit DoEnd IfLoopWhile…Wend循环语句的使用:不建议使用,因为没有退出循环的语句Dim intAgeintAge=0.While intAge<5intAge=intAge+1MsgBox intAgeWendFor…Next循环语句的使用:Dim iFor i=1 To 5MsgBox iNextFor i=1 To 5 Step 2MsgBox iNextFor i=5 To 1 Step -1MsgBox iNext练习:接收用户输入的5个数字,然后倒序输出出来Dim intMyArray(4),iFor i=0 To 4intMyArray(i)=InputBox("请输入第"&CStr(i)&"个数字")NextFor i=4 To 0 Step -1MsgBox "您输入的第"&CStr(i)&"个数字是:"&CStr(intMyArray(i))Next. For each…Next循环语句的使用:如果生命还有三天,你准备怎么安排呢?Dim countDownDaycountDownDay=Array("看日出","骑自行车","聊天")For Each element In countDownDayMsgBox elementNextWith…End With语句的使用:SystemUtil.Run "E:\WINDOWS\system32\calc.exe"With Window("计算器").WinButton("1").Click.WinButton("+").Click.WinButton("2").Click.WinButton("=").Click.CloseEnd withSub与Function的用法:Dim strCallstrCall=InputBox("请输入你想说的话:")'Shout strCallCall Shout(strCall)Sub Shout(ByVal strEcho)MsgBox strEcho&"!"End SubDim strstr=InputBox("请输入你想说的话:")MsgBox Answer(str)Function Answer(ByVal strAsk)Select Case strAskCase "我爱你"Answer="我也爱你"Case "我恨你"Answer="冤冤相报何时了"Case ElseAnswer="下次再聊"End Select. End Function函数的返回值:格式为:函数名=返回值Function add(x,y)add=x+yMsgBox addEnd Functiona=add (1,2)MsgBox a注意与c和tcl中return的区别参数的值传递ByVal与地址传递ByRef:ByVal 与 ByRef(默认值)这两个是子过程的参数传递时,指定参数按什么传递的ByVal(按值传递)ByRef(按地址传递)具体这样来理解:过程中的参数列表,我们称形参调用过程时的参数列表,我们称实参在调用时,我们要将实参的值传递给形参,这样过程才能拿这些数据参与计算并实现一些功能那么在传递的过程中,就存在这两种传递方式传值时(ByVal),是先给形参开辟一个临时地址,将实参的内容传入这个临时地址,这样,传递后,形参与实参是在两上不同的地址中,也就是说他们是相互独立的传址时(ByRef),是直接将实参的地址传递给形参,这样,形参与实参就共用一个地址,所以,形参内容的改变,也直接改变了实参的内容通过上面的分析,你只要记得:按值传递时(ByVal),形参的改变不会影响到实参按址传递时(ByRef),形参的改变,会影响到实参Dim strstr="hello world!"Call strEcho(str)MsgBox strSub strEcho(ByVal str)str=str&"!!!!!"End SubDim strstr="hello world!"Call strEcho(str)MsgBox strSub strEcho(ByRef str)str=str&"!!!!!"End SubDim msgmsg = "喂,你好吗?"MsgBox msgAnswer msgMsgBox msg'Sub Answer(ByVal sentense)' sentense = "我很好!你呢?"'End SubSub Answer(ByRef sentense)sentense = "我很好!你呢?"End Sub过程的调用在调用过程时,不必使用Call关键字。

相关主题