Flexible and practical basic VBS tutorials

Source: Internet
Author: User

We often hear that VBS is powerful and simple. Do you want to know about VBS? Now let's learn about VBS together.

VBS is short for Visual Basic Script Edition. That is to say, VBS is the Script version of Visual Basic. Many people say that VBS is a subset of VB. Because VBS is basically consistent with Visual Basic in programming syntax and functions. Visual Basic can also implement common functions, and the implementation process is easier than Visual Basic, because VBS programming can be completed as long as a text editor, in addition, the Notepad provided by Windows is fully competent.

Let's write a classic routine: hello world. What we want to achieve is the effect in 1. To achieve this effect, we only need one statement. The procedure is as follows: Open notepad and enter "msgBox (" hello world ")" (2) in the editing area ), save the file with the extension VBS (select "all files" in "Save type", and enter the file name including the extension name in "file name", such as "hello world. vbs). After the file is saved, double-click the file to save the effect. Anyone who has learned VB knows that the msgbox function is one of the most commonly used functions in vbprogramming. It can implement pop-up window prompts.


Figure 1


  
Figure 2

If there is output, there will be input. What corresponds to msgBox is the input function InputBox, which is used to receive user input information and then return it to the specified variable. The following is an example. Enter the following content in your notebook:

Dim name
Name = InputBox ("Enter your internet cafe Name", "Name ")
MsgBox (name)

After saving it, double-click it to run. The window shown in 3 is displayed.


Figure 3

Enter the content you want to enter in the window (for example, enter "xiaocao network") and click OK. A Prompt window is displayed, showing the content you entered (4 ).

Figure 4

Now let's have a deep understanding of the significance of the above content. Dim is a keyword for declaring variables in Visual Basic, which is also applicable in VBS. The statement format is as follows:

Dim variable name [As type]

The content in the brackets can be omitted (brackets mentioned in later articles refer to this ). If this parameter is omitted, the declared variable will be a variant. The data type of the variable depends on the value you assign to the variable. For example, if the value you assign to this variable is an integer, then it will be an integer variable. The second statement "Name = InputBox (" Enter your internet cafe Name "," Name ")" indicates that when the InputBox function is executed, the content entered by the user is assigned to the variable Name, "=" is the value assignment symbol, which is different from "=" in numbers ".

The variables mentioned above may be unknown to readers who have not learned programming. In fact, in advanced language programming, there are constants in addition to variables. To put it bluntly, constants and variables are named memory units. When reading and modifying data, they access the data through the memory unit name. Specifically, a variable is the amount of variable that can change its value while the program is running. High flexibility. Constants are constant values in the program running. constants are generally divided into three types: Direct constants and symbolic constants declared by users. Direct constants can be used without being declared. For example, 123 is a direct constant. You can use it directly, but you cannot change it. For example, you need to change it, for example, 123 = 321, it will cause errors when running the program. The constants declared by the user are modified to facilitate reading or modifying the program. The values related to the declared constants will change accordingly, you do not need to modify them one by one. The key word of a constant declaration is Const, for example, const PI = 3.14159. Next we will compile a program for calculating the circular area. Enter the following content in Notepad:

Const PI = 3.14
Dim r, S
R = InputBox ("Enter the radius of the circle you want to calculate", "radius ")
S = PI * r
MsgBox ("the area of the circle is" & s)

Save as a VBS file, and a program for calculating the circular area is complete.

So far, we must have a preliminary understanding of VBS programming. To learn more deeply, we must also learn the selection structure and loop structure, which will be involved in the subsequent articles.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.