Home>

vbscript data type

vbscript has only one data type,This is called variant.varriant is a special data type,Depending on how it is used,It can contain different categories of information.Because variant is the only data type in vbscript,So it is also the data type of the return value of all functions in vbscript.

The simplest variant can contain numeric or string information.variant is treated as a number when used in a numeric context,Treated as a string when used in a string context.That is,If you use data that looks like numbers,Vbscript assumes it is a number and processes it in a way that works for numbers.Similar to this,If the data used can only be strings,Vbscript will be processed as a string.You can also include numbers in quotes ("") to make them strings.

A variant contains a type of value information called a subtype.in most cases,You can put the required data into the variant, and the variant will operate in the way that works best for its data.

The data subtype contained in the variant

vbscript variables and constants

A variable is a convenient placeholder,Used to reference computer memory addresses,This address can store program information that can be changed while the script is running.For example, you can create a variable called clickcount to store the number of times a user clicks on an object on a web page.Using variables does not require knowing the address of the variable in computer memory,As long as you refer to the variable by its name, you can view or more importantly the value of the variable.There is only one basic data type in vbscript,That is variant, so the data type of all variables is variant.

1.The naming rules of variables

Variable naming must follow vbscript's standard naming rules:

The first character must be a letter. Cannot contain embedded periods (.). The length cannot exceed 255 characters. Must be unique within the declared scope. Cannot be the same as vbscript keywords.

2.Declaring variables

In vbscript, variables are usually explicitly declared using dim statements, public statements, and private statements.And allocate storage space.Its syntax is:

{dim | private | public}[,] [,] ... [,

Explanation:(1) Variables declared by the public statement can be used in all procedures in all scripts;

(2) script-level variables declared with dim can be used in all processes in the script,But process energy variables are only used in the process;

(3) A variable declared by a private statement can only be used in a script that declares the variable.

(4) When multiple variables are declared,Use commas to separate variables.Example:dim a, b, c, d

Another way is to declare variables implicitly by using the simple name of the variable name directly in the script.This is usually not a good habit,Because this can sometimes result in unexpected results when running the script due to misspelled variable names.Therefore, it is best to declare all variables using the option explicit statement,And use it as the first statement of the script.(I.e. it must appear before any html logo or other vbscript command,Otherwise, the statement is considered illegal.

Scope and survival of variables

The scope of a variable is determined by where it is declared.If you declare a variable in a procedure,Only the code in the process can access or change the value of the variable,The variable has local scope and is called a process-level variable.If you declare a variable outside the procedure,Then the variable can be recognized by all processes in the script,Called script-level variables,Has script-level scope.

The time that a variable exists is called the survival period.Script-level variables live from the moment they are declared,Until the end of the script.For process-level variables,Its lifetime is only the time the process runs,After the process ends,The variable disappears.During the process,Local variables are ideal for temporary storage.Local variables with the same name can be used in different processes,It is because each local variable is only recognized by the process that declared it.

Assigning values ​​to variablesYou can use an assignment statement to assign a value to a variable.Its format is:

Variable name=value, such as:rsername="shadow"

password="698726as"

birthdate=#1975-12-25 "

5, scalar variables and array variables

In most circumstances,You only need to assign a variable value to the declared variable.A variable that contains only one value is called a scalar variable.In some cases,It is more convenient to assign multiple related values ​​to a variable,So you can create variables that contain a range of values,Called an array variable.The declaration of array and scalar variables is similar,The only difference is when declaring array variables,Variable names are followed by parentheses (). For example:myweekday (6), which declares a one-dimensional array containing 7 elements.

Arrays are not limited to one dimension.The maximum number of dimensions of an array is 60. When a multi-dimensional array is declared,Use commas to separate each number in parentheses that represents the size of the array.For example:dim mytable (5,9), declares a two-dimensional array of 6 rows and 10 columns.

6.Dynamic array

You can also declare dynamic arrays,An array whose size changes when the script is run.Use a dim statement or a redim statement for the initial declaration of a dynamic array. To use dynamic arrays,However, you cannot include numbers in parentheses.You must then use redim to determine the number of dimensions and the size of each dimension.For example:The following redim statement sets the initial size of the dynamic array to 25, and the subsequent redim statement resizes the array to 30, while using the preserve keyword to preserve the contents of the array when resizing.

dim myarray (25)

...

redim anotherarray (30)

7, simple application examples

<html>
<head><title>Application of array</title>
<script language="vbscript" for="button1" event="onclick">
dim myweekday (6)
dim myweek
myweekday (0)="Sunday"
myweekday (1)="Monday"
myweekday (2)="Tuesday"
myweekday (3)="Wednesday"
myweekday (4)="Thursday"
myweekday (5)="Friday"
myweekday (6)="Saturday"
myweek=weekday (date) -1
select case true
case myweek=1
msgbox "Today is"&myweekday (1)&", the beginning of the week,Work hard.
"
case myweek=2
msgbox "Today is"&myweekday (2)&", and still work hard.
"
case myweek=3
msgbox "Today is"&myweekday (3)&", keep working hard.
"
case myweek=4
msgbox "Today is" & myweekday (4) & ", and it will be two weeks before the end of the week.
adhere to. "
case myweek=5
msgbox "Today is"&myweekday (5)&", haha, tomorrow is the week.
"
case myweek=6
msgbox "Today is"&myweekday (6)&", today you can play windy.
"
case myweek=0
msgbox "Today is"&myweekday (0)&", take a good rest,Tomorrow is the start of a new week.
"
end select
</script>
</head>
<body>
<form name=form1>
<input type="button" name="button1" value="Please check what day is today">
</form>
</body>
</html>

vbscript operator

vbscript operators include arithmetic operators,There are several types of join operators, comparison operators, and logical operators.When the expression contains multiple operators.Each part will be calculated in a predetermined order,This order is called operation priority.When using operations, they are executed in the order of parentheses and then parentheses.Standard operator precedence is still required in parentheses;When the priorities are the same, they are calculated from left to right.

1.Arithmetic operatorsVariable naming must follow the standard naming rules of vbscript:

Comparison operators

3.Logical operators

4, the connection operator

5. Operator precedence

asp
  • Previous Train your own PHP coding standards
  • Next Android desktop component App Widget usage introduction tutorial