Vbs learning diary (2)

Source: Internet
Author: User
Tags array definition
VBScript Basics
I. Variables
1, All content after single quotes is interpreted as comments. (CTRL + M comments in vbsedit and CTRL + Shift + M comments in reverse comments)
2. In VBScript, variable naming rules follow standard naming rules. Note the following: Variables, methods, functions, and objects are case-insensitive in VBScript.. When declaring a variable, to explicitly declare a variable, you need to use the dim keyword to tell VBScript that you want to create a variable and keep the variable name behind it. Declare multiple variables of the same type, which can be separated by commas. Note: In VBScript You cannot assign values to a variable when declaring a variable.. However Assign values to both variables in one line of code, separated by colons.
3. You You can use option explicit to tell the host that the variable must be declared first and then used.
4. VBScript only has one variable type. In actual use, you need to use a type conversion function to convert the variable to the corresponding variable type.
The cbool function converts a variable to a Boolean value;
The cbyte function converts a variable to an integer between 0 and 255.
The ccur function, cdbl function, and csng function convert the variable to a floating point value. The former is only accurate to four digits after the decimal point. The latter two are more precise and the value range is greater.

The cdate function converts a variable to a date value.
The CINT and clng functions convert variables to integers. The latter has a larger range than the former.
The CSTR function converts a variable to a string.
2. Array

The definition of an array is very similar to that of a variable. You only need to describe the number and dimension of the array after the variable. Note that the subscript of an array always starts from 0 and ends with a minus one in the array definition. That is to say, to define an array with ten data records, you will write the code as follows: dim array (9). Similarly, when you want to access the fifth element, the actual code is arr ay (4 ). Of course, you can declare a dynamic array without specifying the number and dimension of the array. When the number and dimension of the array are fixed, use the keywordRedim to change the Array. Note: When the array size is changed, the data in the array will be damaged.Keyword preserve to protect data.

For example:

Redim space preserve space array parentheses count comma-dimensional brackets

Iii. Operators
In VBScript operators, addition, subtraction, multiplication, division, and division are all common symbols. The multiplication operator uses ^, And the modulo operation uses Mod. In comparison operators, equal to, less than, greater than, less than or equal to, greater than, and so on are all consistent with commonly used symbols. Not equal to less than or greater than used for connection.
Logical operators: And operations-> and non-operations-> not or operations-> or;
You can use Operator + and operator & to connect strings, generally use & operator;
There is also a special Operator is used to compare objectsSuch as button object, if The object is of the same type and the result is true. If the object is not of the same type, the result is false..

Iv. Condition statements

Mainly include if ...... Then statement and selectcase statement

In if ...... In the then statement, the basic form is:
If condition then
Statement of the processing condition;
......

Endif

The basic form can only verify a single condition. If there are two conditions, you need to add a single row statement else in the basic form. If there are more conditions to be verified, you need to add the elseif condition then Processing Condition Statement.
In the selectcase statement, the basic form is:
Select case variable
Case condition Value
Processing Condition Statement
And repeat the previous two sentences.
The last sentence should be
Case else

Processing statement
Of course, do not forget to put the end select clause in the last row.
Note: When performing string comparison, pay special attention to Case sensitivity. Generally, before comparison, Use the lcase function to convert the string to lowercase, and use the ucase function to convert the string to uppercase..
V. cyclic control statements
Loop Control statements include ...... Next loop, ...... Each loop, do ...... While loop, do ...... Until loop and while loop.
Before using a loop control statement, you must first determine the loop conditions. If the number of loops is fixed, use ...... Next loop with the following structure:
For counter variable = start Count value to last count value
Execution loop body
Next
To determine each element in an array or object set, use ...... The structure of an each loop is as follows:
For each: the object or array to be viewed by the in variable for cyclic counting
Execute the processing statement
Next
Note: You can use exit for to exit the loop at any time in the two loops.
If you want When conditions are metExecute a piece of code and use do ...... While statement, structure:
Do While Condition
Execution loop body
Loop
If you want When the conditions are not metRun the code and use do ...... The until statement in the following structure:
Do until Condition
Execution loop body
Loop
Of course, In these two loop statements, you can use exit do to exit the loop.The last loop statement is Always execute a loop when conditions are met,
While Condition
Execution loop body
Wend
Vi. Use Process
There are two common processes: a function, a return value for the caller, a subroutine, no return value, and a call Special event subroutines.
The basic function definition method is as follows:
Function Name (parameter list)
Function Code
Function name = A value 'is used to return values.
End Function
Some subprograms are similar, but there is no return value
Note: Although the parameter list must be enclosed in parentheses when defining a subroutine, the parameter list is not added when the subroutine is called.Brackets are used only in functions. In addition, Subprograms cannot be used in expressions.. The function can only appear on the right of the value assignment statement or in an expression, Functions cannot be used directly.If the function must be used directly Must be called using the Call Statement and the return value is canceled.

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.