⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vbs15.htm

📁 VBScript 是一种脚本语言
💻 HTM
字号:
<HTML>
<HEAD>
<TITLE>VBScript Variables</TITLE> 
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso8859-1">
<META NAME="MS.LOCALE" CONTENT="EN-US">
<META NAME="PRODUCT" CONTENT="Visual Basic Scripting Edition">
<META NAME="TECHNOLOGY" CONTENT="SCRIPTING">
<META NAME="CATEGORY" CONTENT="Tutorial">

<META NAME="Description" CONTENT="VBScript Variables">
</HEAD>
<BODY BGCOLOR=FFFFFF LINK=#0033CC>
<!--TOOLBAR_START-->
<!--TOOLBAR_EXEMPT-->
<!--TOOLBAR_END-->
<FONT FACE="Verdana, Arial, Helvetica" SIZE=2>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%><TR VALIGN=TOP><TD WIDTH=360>
<FONT SIZE=1 COLOR=#660033>Microsoft&#174; Visual Basic&#174; Scripting Edition</FONT><BR>
<FONT SIZE=5 COLOR=#660033><B>VBScript Variables</B></FONT>

</TD>
<TD ALIGN=RIGHT>
<FONT SIZE=2>&nbsp;<A HREF="vbstutor.htm">VBScript&nbsp;Tutorial</A>&nbsp;<BR>&nbsp;<A HREF="vbs6.htm">Previous</A>&nbsp;|&nbsp;<A HREF="vbs5.htm">Next</A>&nbsp;<P></FONT>


</TD></TR>
</TABLE> 
&nbsp;<BR>

<HR NOSHADE SIZE=1>

<H5>What Is a Variable?</H5>
<BLOCKQUOTE>
A variable is a convenient placeholder that refers to a computer memory location where you can store program information that may change during the time your script is running.  For example, you might create a variable called <FONT FACE="Courier New" SIZE=2>ClickCount</FONT> to store the number of times a user clicks an object on a particular Web page. Where the variable is stored in computer memory is unimportant. What's important is that you only have to refer to a variable by name to see its value or to change its value. In VBScript, variables are always of one fundamental data type, <A HREF="vbs6.htm"><B>Variant</B></A>.
</BLOCKQUOTE>

<H5>Declaring Variables</H5>
<BLOCKQUOTE>
You declare variables explicitly in your script using the <A HREF="vbs571.htm"><B>Dim</B></A> statement, the <a href="vbs591.htm"><b>Public</b></a> statement, and the <a href="vbs589.htm"><b>Private</b></a> statement. For example:
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Dim DegreesFahrenheit</FONT></PRE></BLOCKQUOTE>

You declare multiple variables by separating each variable name with a comma. For example:

<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Dim Top, Bottom, Left, Right</FONT>
</PRE></BLOCKQUOTE>
You can also declare a variable implicitly by simply using its name in your script. That's not generally a good practice because you could misspell the variable name in one or more places, causing unexpected results when your script is run. For that reason, the <A HREF="vbs588.htm"><B>Option Explicit</B></A> statement is available to require explicit declaration of all variables. The <B>Option Explicit</B> statement should be the first statement in your script.
</BLOCKQUOTE>

<H5>Naming Restrictions</H5>
<BLOCKQUOTE>Variable names follow the standard rules for naming anything in VBScript. A variable name:
<UL>
<LI>Must begin with an alphabetic character.
<LI>Cannot contain an embedded period.
<LI>Must not exceed 255 characters.
<LI>Must be unique in the scope in which it is declared.
</UL></BLOCKQUOTE>

<H5>Scope and Lifetime of Variables</H5>
<BLOCKQUOTE>
A variable's scope is determined by where you declare it. When you declare a variable within a procedure, only code within that procedure can access or change the value of that variable. It has local <A HREF="vbs0.htm#defScoping">scope</A> and is called a <A HREF="vbs0.htm#defProcLevel">procedure-level</A> variable. If you declare a variable outside a procedure, you make it recognizable to all the procedures in your script. This is a <A HREF="vbs0.htm#defScriptLevel">script-level</A> variable, and it has script-level scope.<P>

How long a variable exists is its lifetime. The lifetime of a script-level variable extends from the time it's declared until the time the script is finished running. At procedure level, a variable exists only as long as you are in the procedure. When the procedure exits, the variable is destroyed. Local variables are ideal as temporary storage space when a procedure is executing. You can have local variables of the same name in several different procedures because each is recognized only by the procedure in which it is declared.
</BLOCKQUOTE>

<H5>Assigning Values to Variables</H5>
<BLOCKQUOTE>
Values are assigned to variables creating an expression as follows: the variable is on the left side of the expression and the value you want to assign to the variable is on the right. For example:

<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> B = 200</FONT></PRE></BLOCKQUOTE></BLOCKQUOTE>

<H5>Scalar Variables and Array Variables</H5>
<BLOCKQUOTE>
Much of the time, you just want to assign a single value to a variable you've declared. A variable containing a single value is a scalar variable. Other times, it's convenient to assign more than one related value to a single variable. Then you can create a variable that can contain a series of values. This is called an array variable. Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses ( ) following the variable name. In the following example, a single-dimension array containing 11 elements is declared:

<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Dim A(10)</FONT></PRE></BLOCKQUOTE>

Although the number shown in the parentheses is 10, all <A HREF="vbs0.htm#defArray">arrays</A> in VBScript are zero-based, so this array actually contains 11 elements. In a zero-based array, the number of array elements is always the number shown in parentheses plus one. This kind of array is called a fixed-size array.<P>

You assign data to each of the elements of the array using an index into the array. Beginning at zero and ending at 10, data can be assigned to the elements of an array as follows:

<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> A(0) = 256
 A(1) = 324
 A(2) = 100
 . . .
 A(10) = 55
</FONT></PRE></BLOCKQUOTE>

Similarly, the data can be retrieved from any element using an index into the particular array element you  want. For example:

<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> . . . 
 SomeVariable = A(8)  
 . . . 
</FONT></PRE></BLOCKQUOTE>

Arrays aren't limited to a  single dimension. You can have as many as 60 dimensions, although most people can't comprehend more than three or four dimensions. Multiple dimensions are declared by separating an array's size numbers in the parentheses with commas.  In the following example, the <FONT FACE="Courier New" SIZE=2>MyTable</FONT> variable is a two-dimensional array consisting of 6 rows and 11 columns:

<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Dim MyTable(5, 10)
</FONT></PRE></BLOCKQUOTE>
In a two-dimensional array, the first number is always the number of rows; the second number is the number of columns.<P>

You can also declare an array whose size changes during the time your script is running. This is called a dynamic array. The array is initially declared within a procedure using either the <B>Dim</B> statement or using the <A HREF="vbs595.htm"><B>ReDim</B></A> statement. However, for a dynamic array, no size or number of dimensions is placed inside the parentheses. For example:

<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Dim MyArray()
 ReDim AnotherArray()</FONT></PRE></BLOCKQUOTE>

To use a dynamic array, you must subsequently use <B>ReDim</B> to determine the number of dimensions and the size of each dmension. In the following  example, <B>ReDim </B> sets the initial size of the dynamic array to 25. A subsequent <B>ReDim</B> statement resizes the array to 30, but uses the <B>Preserve</B> keyword to preserve the contents of the array as the resizing takes place.

<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> ReDim MyArray(25)
 . . . 
 ReDim Preserve MyArray(30)</FONT></PRE></BLOCKQUOTE>

There is no limit to the number of times you can resize a dynamic array, but you should know that if you make an array smaller than it was, you lose the data in the eliminated elements.
</BLOCKQUOTE>

<hr noshade size=1>
<p align=center><em><a href="../../common/colegal.htm">&copy; 1997 by Microsoft Corporation. All rights reserved.</a></em></p> 
</FONT>
</BODY>
</HTML>







































⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -