Visual Basic Script program reference manual - day 2: start with a few simple codes and learn the basic and syntax of VBScript

Keywords: Algorithm Machine Learning AI VBScript

Study several DEMO

The first lecture should be basic grammar, but it seems too boring to speak seriously. Here are a few examples.

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 is the keyword used to declare variables.
In English, the separator is a comment symbol, followed by the comment content.
InputBox() is a built-in function that pops up an input dialog box. The characters in brackets are the output content of the dialog box. At the same time, a text editing box is automatically generated below to input content. The input content will be assigned to the defined argument "YourName";

MsgBox is a built-in function. It doesn't matter whether you use parentheses or not. That's why VB is simple.
"&" is a connector used to connect strings. The parameter immediately following Msgbox is the first parameter, which will be displayed in the message dialog box in the future.

DEMO2

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

First, a variable, Some Number, is defined. Before assignment, no one knows what type the variable is. This is the characteristic of VB: use it casually.
Until an integer 999. Some Number is assigned below, it becomes an integer. After calculation, somnumber becomes 2000; Output through the message dialog box.

DEMO 3

Dim SomeNumber 
SomeNumber = 999
SomeNumber = SomeNumber + 2
SomeNumber = SomeNumber + 999
SomeNumber = "The shore of Wen River"
MsgBox SomeNumber

At first, SomNumber is an integer, and then it becomes a string. Debugging will be troublesome in the future. Vbsctrip only supports one data type - Variant, and there is no need to specify variables and functions as some data type.

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

Here comes the judgment branch structure

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

Indentation is not necessary in VBS, but it is better for the readability of the program.

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 is executed at least once. When XXX meets the conditions, SSSS will always be executed; There are many circular sentence structures about VBS. It's clear to see them in the reference book.

DEMO 6

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

This example is also very simple, that is, the simple loop structure is the same as other grammars.

DEMO 7

Dim Counter
Dim WordLength
Dim InputWord
Dim WordBuilder
InputWord = InputBox ("Type in a word of phrase to use")
WordLength = Len(InputWord)        'Len() Built in function to find the string length
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) built-in function to take the n characters starting from the m-th character in the string Words.

DEMO 8

Dim PartingGreeting
Dim VisitorName
VisitorName = PromptUserName 'A function is called here.
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  'Definition of function  Function start
    ' 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

In VBS, functions do not need to define input and output parameters, and the function name can be used as parameters. It's really eye opening.

Posted by Cynix on Sun, 21 Nov 2021 03:18:31 -0800