当前位置: 首页 > 网络学院 > 客户端脚本教程 > VBScript > VBScript 变量

VBScript
VBScript 介绍
如何使用 VBScript
VBScript 放置
VBScript 变量
VBScript 程序
VBScript 条件语句
VBScript 循环声明
VBScript 摘要
VBScript 实例
VBScript 函数
VBScript 关键字

VBScript 变量


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 629 ::
收藏到网摘: n/a

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?
什么是变量?

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.
当你在一程序里声明了一个变量,那么变量的可访问性只局限于这个程序之内。当这段程序结束,变量也将被销毁。这种变量可称为局部变量。你在不同的程序里使用相同的变量名,是不会产生影响的。

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

登陆 | 还没注册?