Visual Basic Script 程序参考手册-学习第2天:从几个简单的代码开始,学习VBScript的基本与语法

Visual Basic Script 程序参考手册-学习第2天:从几个简单的代码开始,学习VBScript的基本与语法

目录

研究几个DEMO

第一讲应该是基本语法,但是一本正经的讲好像太枯燥,下面就从几个例子来看。

DEMO1

Dim YourName   ' dimension the variable 
YourName = InputBox("Hello!  What is your name?")  ' ask for the user's name
MsgBox "Hello " & YourName & " !  Pleased to meet you." ' display a greeting;

Dim 是关键字 用来声明变量的。
在英文状态下的分隔号是注释符号,后面的都是注释内容。
InputBox() 为内建函数,弹出输入对话框,括号中的字符就是对话框的输出内容,同时下面自动生成一个文本编辑框就是用来输入内容的。输入的内容会赋值给定义的自变量“YourName”;
Visual Basic Script 程序参考手册-学习第2天:从几个简单的代码开始,学习VBScript的基本与语法
MsgBox 是内建函数,用不用括号都无所谓,这就是VB简单的原因。
“&”为连接符,用来把字符串连接起来。Msgbox 后面紧跟的参数第一个参数,将来显示在消息对话框中。
Visual Basic Script 程序参考手册-学习第2天:从几个简单的代码开始,学习VBScript的基本与语法

DEMO2

Dim SomeNumber 
SomeNumber = 999
SomeNumber = SomeNumber + 2
SomeNumber = SomeNumber + 999
MsgBox SomeNumber

首先定义了一个变量,Some Number,在没有赋值以前,谁也不知道这个变量是什么类型的。这就是VB的特点:随便用。
直到下下面赋值了一个整数999. Some Number就变成了整数。经过运算以后SomNumer变成了2000;通过消息对话框输出。

Visual Basic Script 程序参考手册-学习第2天:从几个简单的代码开始,学习VBScript的基本与语法

DEMO 3

Dim SomeNumber 
SomeNumber = 999
SomeNumber = SomeNumber + 2
SomeNumber = SomeNumber + 999
SomeNumber = "汶河之滨"
MsgBox SomeNumber

刚开始SomNumber为整数,后面又变成了字符串。将来调试的时候会比较麻烦,VBSctript只支持一种数据类型——Variant,无需将变量和函数指定为某种数据类型。
Visual Basic Script 程序参考手册-学习第2天:从几个简单的代码开始,学习VBScript的基本与语法

DEMO4

Dim YourName 
Dim Greeting
YourName = InputBox("Hello!  What is your name?")
If YourName = "" Then
    Greeting = "OK.  You don't want to tell me your name."
Else
    Greeting = "Hello, " & YourName & ", great to meet you."
End If

这里开始出现了判断分支结构

// A  do BLOCK
IF XXX THEN
	SSSSS
Else
	SSSSS
END IF 

在VBS 中缩进是非必须的,但是为了程序的易读性还是缩进的比较好。

DEMO5

Dim Greeting
Dim YourName
Dim TryAgain
Do
    TryAgain = "No"
    YourName = InputBox("Please enter your name:")
    If YourName = "" Then
        MsgBox "You must enter your name to continue."
        TryAgain = "Yes"
    Else
        Greeting = "Hello, " & YourName & ", great to meet you."
    End If
Loop While TryAgain = "Yes"
MsgBox Greeting
// A CODE BLOCK
DO  SSSS  Loop While  XXXX

SSSS至少被执行一次。当XXX满足条件的时候会一直执行SSSS;关于VBS的循环语句结构还有很多,去参考书中看看就很清楚了。

DEMO 6

Dim Counter
MsgBox "Let's count to ten. Ready?"
For Counter = 1 to 10
    MsgBox Counter
Next
MsgBox "Wasn't that fun?"

这个例子也很简单就是简单的循环结构和其他语法都是相同的。

DEMO 7

Dim Counter
Dim WordLength
Dim InputWord
Dim WordBuilder
InputWord = InputBox ("Type in a word of phrase to use")
WordLength = Len(InputWord)        ’Len() 内建函数,求字符串长度
For Counter = 1 to WordLength     
    MsgBox Mid(InputWord, Counter, 1)
    WordBuilder = WordBuilder & Mid(InputWord, Counter, 1)
Next
MsgBox WordBuilder & " contains " & WordLength & " characters."

Mid(Words,m,n) 内建函数,取字符串Words中第m个字符开始的n个字符。

DEMO 8

Dim PartingGreeting
Dim VisitorName
VisitorName = PromptUserName ‘这里调用了一个函数。
If VisitorName <> "" Then
    PartingGreeting = "Goodbye, " & VisitorName & ". Nice to have met you."
 Else
    PartingGreeting = "I'm glad to have met you, but I wish I knew your name."
End If
MsgBox PartingGreeting
Function PromptUserName  ‘函数的定义  Function开头
    ' This Function prompts the user for their name.
    ' It incorporates various greetings depending on input by the user.
    Dim YourName 
    Dim Greeting
    YourName = InputBox("Hello!  What is your name?")
    If YourName = "" Then
        Greeting = "OK.  You don't want to tell me your name."
    ElseIf YourName = "abc" Then 
        Greeting = "That's not a real name."	
    ElseIf YourName = "xxx" Then 
        Greeting = "That's not a real name."
    Else
        Greeting = "Hello, " & YourName & ", great to meet you."    
        If YourName = "Fred" Then
            Greeting = Greeting & "  Nice to see you Fred."
        End If
    End If
    MsgBox Greeting
    PromptUserName = YourName
End Function

VBS中,函数都不需要定义输入输出参数,函数名就能当参数用,真是开了眼了。

上一篇:【ubuntu20】增加root用户


下一篇:了解编程语言