ASP Introductory Tutorials-vbscript basic elements

Source: Internet
Author: User
Tags numeric variables range scalar


VBScript data type



VBScript has only one data type, variant, called Variant type. Varriant is a special type of data that, depending on how it is used, can contain different categories of information. Because the variant is the only data type in VBScript, it is also the data type of the return value of all functions in VBScript.



The simplest variant can contain numeric or string information. A Variant is used as a numeric processing in a numeric context and is used as a string in the context of a string. That is, if you use data that looks like a number, VBScript assumes it is a number and handles it in a digital way. Similarly, if the data used is only a string, VBScript will be treated as a string. You can also include a number in quotation marks ("") to make it a string.



A Variant contains a value information type called a subtype. In most cases, you can put the data you want into a variant, and the variant will operate in the way that best applies to its data.


Variant contains the data subtype
Sub Type Description
Boolean Contains true or False
Byte Contains integers between 0 and 255
Currency -922,337,203,685,477.5808 to 922,337,203,685,477.5808
Long Contains-2,147,483,648 to 2,147,483,648 integers
Single Contains a single-precision floating-point number with a negative range from -3.402823E38 to -1.401298E-45 and a positive range from 1.401298E-45 to 3.402823E38
Double Contains a double-precision floating-point number with a negative range from -1.79769313486232E308 to -4.94065645841247E-324, with a positive range from 4.94065645841247E-324 to 1.79769313486232E308
Date (Time) Contains a number that represents a date ranging from January 1, 100 A.D. to December 9999 Day
String Contains variable-length strings with a maximum length of 2 billion characters
Object Include Object
Error Contains the error number
Integer Contains integers between 32,768 and 32,767


VBScript variables and constants



A variable is a handy placeholder for referencing a computer's memory address that can store program information that can be changed while the script is running. For example, you can create a variable named Clickcount to store the number of times a user clicks an object on a Web page. Variables do not need to understand the address of the variable in the computer's memory, as long as the variable name refers to the variables can be viewed or more required variable values. There is only one basic data type in VBScript, a variant, so all variables have a variant of the data type.



1, the variable naming rules



Variable naming must follow VBScript's standard naming rules:


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


2. Declaring variables



In VBScript, variables are typically explicitly declared with dim statements, public statements, and private statements, and storage space is allocated. The syntax format is:



{dim| private| Public} < variable name 1> [,< variable name 2>] [,< variable name 3> [,< variable name;



Description



(1) A variable declared by the public statement can be used for all procedures in all scripts;



(2) The script-level variable declared with Dim can be used in all the processes in the scripts, but the process energy variable is used only in the process;



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



(4) When declaring multiple variables, use commas to separate the variables. such as: Dim a,b,c,d



Another way is to implicitly declare a variable by using the variable name directly in the script. This is usually not a good habit, because it can sometimes cause unexpected results when the script is run because the variable name is spelled incorrectly. Therefore, it is best to use the Option Explicit statement to declare all variables as the first statement of the script. (that is, it must appear before any HTML ID or other VBScript command, otherwise the statement will be treated as an illegal statement.)



3, the scope of the variable and the survival period



The scope of a variable is determined by the position in which it is declared. If you declare a variable in a procedure, only the code in the procedure can access or change the value of the variable, at which point the variable has a local scope and is called a procedure-level variable. If you declare a variable outside of a procedure, the variable can be recognized by all procedures in the script, called a script-level variable, with a script-level scope.



The time at which a variable exists is called a survival period. The lifetime of a script-level variable is from the moment it is declared until the end of the script run. For a procedure-level variable, its lifetime is only the time that the process is running, and the variable disappears after the process is closed. Local variables are ideal temporary storage space when executing a procedure. Local variables with the same name can be used in different procedures because each local variable is recognized only by the process that declares it.



4. Assigning values to variables



You can assign a value to a variable by using an assignment statement in the following format:



Variable name = value such as: Rsername= "Shadow"



Password= "698726as"



birthdate= #1975-12-25 "



5, scalar variables and array variables



In most cases, you only need to assign a variable value to a variable that you declare. 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 a variable that contains a series of values, called an array variable. The declaration of an array variable and a scalar variable is similar, and the only difference is that when declaring an array variable, the variable name is followed by parentheses (). For example: Myweekday (6), which declares a one-dimensional array that contains 7 elements.



Arrays are not limited to one dimension. The maximum number of dimensions for an array can be 60, and when you declare a multidimensional array, use commas to separate each number in parentheses that represents the size of the array. For example: Dim MyTable (5,9), which declares a two-dimensional array of 6 rows and 10 columns.



6, dynamic array



You can also declare a dynamic array, which is the size of the array when the script is run. The initial declaration of a dynamic array uses a Dim statement or a ReDim statement. To use a dynamic array, you cannot include numbers in parentheses. You must then use ReDim to determine the 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 ReDim statement resizes the array to 30, using the Preserve keyword to retain the contents of the array when resizing.



Dim MyArray (25)



......



ReDim Anotherarray (30)



7. Simple Application Example


<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, hard work yo. "
Case myweek=2
Msgbox "Today is" & Myweekday (2) & ", but also to work hard yo." "
Case Myweek=3
Msgbox "Today is" & Myweekday (3) & ", continue to work hard yo." "
Case myweek=4
Msgbox "Today is" & Myweekday (4) & ", there are two days to week unfinished, insist." "
Case Myweek=5
Msgbox "Today is" & Myweekday (5) & ", Haha, tomorrow is weeks." "
Case Myweek=6
Msgbox "Today is" & Myweekday (6) & ", you can play the wind crazy today." "
Case Myweek=0
Msgbox "Today is" & Myweekday (0) & ", have a good rest, tomorrow is the beginning of a new week." "
End Select
</script>
<body>
<form name=form1>
<input type= "button" Name= "button1" value= "please see what day of the Week" >
</form>
</body>
Related Article

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.