ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: VBScript :: VBScript ±äÁ¿

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

VBScript
VBScript ½éÉÜ
ÈçºÎʹÓà VBScrip..
VBScript ·ÅÖÃ
VBScript ±äÁ¿
VBScript ³ÌÐò
VBScript Ìõ¼þÓï¾..
VBScript Ñ­»·ÉùÃ..
VBScript ÕªÒª
VBScript ʵÀý
VBScript º¯Êý
VBScript ¹Ø¼ü×Ö

VBScript ±äÁ¿


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ä¯ÀÀ:2085 :: ::

Examples
ʵÀý

Create a variable
Variables are used to store information. This example demonstrates how you can create a variable, and assign a value to it.
±äÁ¿¿ÉÓÃÀ´´æ´¢ÐÅÏ¢¡£Õâ¸öʵÀý½«ÑÝʾÈçºÎ½¨Á¢±äÁ¿£¬²¢¸øËü·ÖÅäÒ»¸öÖµ¡£

Insert a variable value in a text
This example demonstrates how you can insert a variable value in a text.
ÔõÑù¸øÒ»¸ö±äÁ¿·ÖÅäÎÄ×Ö

Create an array
Arrays are used to store a series of related data items. This example demonstrates how you can make an array that stores names. ( We are using a "for loop" to demonstrate how you write the names. )
Êý×éÓÃÀ´´æ·ÅһϵÁÐÏà¹ØµÄÊý¾Ý¡£Õâ¸öÀý×Ó½«ÑÝʾÈçºÎ½¨Á¢´æ·ÅÁ˶à¸öÃû×ÖµÄÊý×é(ÎÒÃÇʹÓÃÁË"for Ñ­»·"À´½«ÕâЩÃû×ÖÒ»Ò»ÏÔʾ)


What is a Variable?
ʲôÊDZäÁ¿£¿

A variable is a "container" for information you want to store. A variable's value can change during the script. You can refer to a variable by name to see its value or to change its value. In VBScript, all variables are of type variant, that can store different types of data.
±äÁ¿ÀàËÆÓÚ´æ·ÅÐÅÏ¢µÄ"ÈÝÆ÷"¡£Ò»¸ö±äÁ¿Öµ¿ÉÒÔÔڽű¾ÖнøÐиı䡣Äã¿ÉÒÔͨ¹ýÕâ¸ö±äÁ¿ÃûÀ´²é¿´ËüËùЯ´øµÄÖµ»òÊǸıäËüËùЯ´øµÄÖµ¡£ËùÓбäÁ¿µÄÀàÐͶ¼¿ÉÒԸıäµÄ£¬Òò´Ë¿ÉÒÔ´æ·Å²»Í¬ÀàÐ͵ÄÊý¾Ý¡£


Rules for Variable Names:
±äÁ¿ÃûµÄ¹æÔò£º

  • Must begin with a letter
    Ãû³ÆµÄ¿ªÍ·±ØÐëΪ×Öĸ
  • Cannot contain a period (.)
    ²»ÄÜ´øÓоäºÅ(.)
  • Cannot exceed 255 characters
    ³¤¶È²»Äܳ¬¹ý255¸ö×Ö·û

Declaring Variables
ÉùÃ÷±äÁ¿

You can declare variables with the Dim, Public or the Private statement. Like this:
Äã¿ÉÒÔʹÓÃDim£¬Public»òÊÇPrivateÀ´ÉùÃ÷Ò»¸ö±äÁ¿£¬±ÈÈçÏñÕâÑù£º

dim name
name=some value

Now you have created a variable. The name of the variable is "name".
ÏÖÔÚÄãÒѾ­½¨Á¢ÁËÒ»¸öÃûΪ"name"µÄ±äÁ¿¡£

You can also declare variables by using its name in your script. Like this:
Ä㻹¿ÉÒÔÔڽű¾ÖÐͨ¹ýʹÓÃÕâ¸öÃû³ÆÀ´Ö±½ÓÉùÃ÷Õâ¸ö±äÁ¿£¬ÏñÕâÑù£º

name=some value

Now you have also created a variable. The name of the variable is "name".
ÏÖÔÚÄãͬÑù½¨Á¢ÁËÒ»¸ö±äÁ¿£¬Ãû³ÆÎª"name"

However, the last method is not a good practice, because you can misspell the variable name later in your script, and that can cause strange results when your script is running. This is because when you misspell for example the "name" variable to "nime" the script will automatically create a new variable called "nime".  To prevent your script from doing this you can use the Option Explicit statement. When you use this statement you will have to declare all your variables with the dim, public or private statement. Put the Option Explicit statement on the top of your script. Like this:
È»ºóÏà¶ÔÓÚµÚÒ»ÖÖ·½·¨£¬µÚ¶þÖÖ·½·¨²»Ì«¿ÆÑ§£¬ÒòΪÔÚºóÃæµÄ½Å±¾ÖÐÄãºÜÓпÉÄܻᷢÉúƴд´íÎóµÄÇé¿ö£¬Õâ¾Í»á¸øÄãµÄ½Å±¾ÔÚÔËÐÐʱ´øÀ´Ææ¹ÖµÄ½á¹û¡£´ò¸ö±È·½Èç¹ûÄ㽫"name"±äÁ¿´ò³ÉÁË"nime"ÄÇô½Å±¾»á×Ô¶¯Éú³ÉÒ»¸öÃûΪ"nime"µÄ±äÁ¿¡£Òª·ÀÖ¹ÕâÑùµÄ´íÎó²úÉúÄã¿ÉÒÔʹÓÃOption ExplicitÓï¾ä¡£ÕâÑù¾Í¿ÉÒÔÆÈʹÄãʹÓÃdim,public»òÊÇprivateÓï¾äÀ´ÉùÃ÷±äÁ¿ÁË¡£½«Õâ¾ä»°·ÅÔڽű¾µÄ¿ªÍ·£¬ÏñÕâÑù£º

option explicit
dim name
name=some value


Assigning Values to Variables
¸ø±äÁ¿¸³Öµ

You assign a value to a variable like this:
Äã¿ÉÒÔÏñÕâÑù¸øÄãµÄ±äÁ¿¸½Öµ£º

name="Hege"
i=200

The variable name is on the left side of the expression and the value you want to assign to the variable is on the right. Now the variable "name" has the value "Hege".
ÔÚ±í´ïʽÖбäÁ¿ÃûλÓÚ×ó±ß£¬ÄãËùÏëÒª¸³Óè±äÁ¿µÄÖµÔÚÓұߡ£¿ÉÒÔÀí½âΪ±äÁ¿"name"ÖеÄֵΪ"Hege"


Lifetime of Variables
±äÁ¿µÄ´æ»îʱ¼ä

How long a variable exists is its lifetime.
±äÁ¿µÄµÄÈ·Çдæ»îʱ¼äΪ¶à³¤£¿

When you declare a variable within a procedure, the variable can only be accessed within that procedure. When the procedure exits, the variable is destroyed. These variables are called local variables. You can have local variables with the same name in different procedures, because each is recognized only by the procedure in which it is declared.
µ±ÄãÔÚÒ»³ÌÐòÀïÉùÃ÷ÁËÒ»¸ö±äÁ¿£¬ÄÇô±äÁ¿µÄ¿É·ÃÎÊÐÔÖ»¾ÖÏÞÓÚÕâ¸ö³ÌÐòÖ®ÄÚ¡£µ±Õâ¶Î³ÌÐò½áÊø£¬±äÁ¿Ò²½«±»Ïú»Ù¡£ÕâÖÖ±äÁ¿¿É³ÆÎª¾Ö²¿±äÁ¿¡£ÄãÔÚ²»Í¬µÄ³ÌÐòÀïʹÓÃÏàͬµÄ±äÁ¿Ãû£¬ÊDz»»á²úÉúÓ°ÏìµÄ¡£

If you declare a variable outside a procedure, all the procedures on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed.
Èç¹ûÄãÔÚ³ÌÐòÍâÉùÃ÷Á˱äÁ¿£¬ÄÇôÔÚÕâ¸öÒ³µÄËùÓгÌÐò¶¼ÄÜ·ÃÎʵ½Õâ¸ö±äÁ¿¡£Õâ¸ö±äÁ¿ÔڹرÕÕâ¸öÒ³ÃæÇ°¶¼ÊÇÓÐЧµÄ¡£


Array Variables
Êý×é±äÁ¿

Sometimes you want to assign more than one value to a single variable. Then you can create a variable that can contain a series of values. This is called an array variable. The declaration of an array variable uses parentheses ( ) following the variable name. In the following example, an array containing 3 elements is declared:
ÓÐʱºòÏëÒª¸øµ¥¶ÀµÄ±äÁ¿¸³Óè¶à¸öÖµ¡£Õâ¸öʱºòÄã¿ÉÒÔ½¨Á¢Ò»¸ö°üº¬Ò»ÏµÁÐÖµµÄ±äÁ¿¡£Õâ¾ÍÊÇÊý×é±äÁ¿¡£ÒªÉùÃ÷Ò»¸öÊý×é±äÁ¿¿ÉÒÔÔÚ±äÁ¿Ãûºó¼ÓÉÏÒ»¶ÔÀ¨ºÅ£¬ÏÂÃæÕâ¸ö¾ÙÀýÉùÃ÷Á˰üº¬Èý¸öÔªËØµÄÒ»¸öÊý×é±äÁ¿£º

dim names(2)

The number shown in the parentheses is 2. We start at zero so this array contains 3 elements. This is a fixed-size array. You assign data to each of the elements of the array like this:
ÔÚÀ¨ºÅÀïµÄÊý×ÖΪ2¡£ÎÒÃÇ´Ó0¿ªÊ¼Êý£¬Òò´ËÕâ¸öÊý×é¿ÉÒÔ°üº¬Èý¸öÔªËØ¡£ÕâÊÇÒ»¸ö¹Ì¶¨´óСµÄÊý×飬Äã¿ÉÒÔ½«Êý¾ÝÒ»Ò»¶ÔÓ¦µ½Ã¿¸öÔªËØÀÏñÕâÑù£º

names(0)="Tove"

names(1)="Jani"
names(2)="Stale"

Similarly, the data can be retrieved from any element using the index of the particular array element you want. Like this:
ÓÉ´Ë¿ÉÒÔ·´¹ýÀ´µÃµ½Ã¿¸öÔªËØÀïµÄÖµ£¬ÏñÕâÑù£º

mother=names(0)

You can have up to 60 dimensions in an array. Multiple dimensions are declared by separating the numbers in the parentheses with commas. Here we have a two-dimensional array consisting of 5 rows and 7 columns:
Äã×î¶à¿ÉÒÔ½¨Á¢Ò»¸ö60άÊý×é¡£¶àάÊý×é¿ÉÒÔÓÃÒÔ¶ººÅ·Ö¸ôµÄÊý×ÖÀ´ÉùÃ÷Ëü¡£ÕâÀïÎÒÃǽ¨Á¢ÁËÒ»¸ö¶þάÊý×飬Ëü°üº¬5ÐÐ7ÁУº

dim table(4, 6)

ÆÀÂÛ (0) All