18.01.2013 Views

VBScript Reference Manual for InduSoft Web Studio

VBScript Reference Manual for InduSoft Web Studio

VBScript Reference Manual for InduSoft Web Studio

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>VBScript</strong> <strong>Reference</strong> <strong>Manual</strong> <strong>InduSoft</strong> <strong>Web</strong> <strong>Studio</strong><br />

Variable Scope<br />

All <strong>VBScript</strong> variables have “scope”. Scope defines a variable’s visibility or accessibility from one<br />

procedure (or <strong>VBScript</strong> Interface) to another, which in IWS is principally determined by where you<br />

declare the variable. As a general rule, when you declare a variable within a procedure, only code<br />

within that procedure can access or change the value of that variable. This is called local scope and is<br />

<strong>for</strong> a procedure-level variable.<br />

If you declare a variable outside a procedure, you make it recognizable to all the procedures in your<br />

Script. This is a Script-level variable, and it has Script-level scope. However, as previously noted,<br />

<strong>InduSoft</strong> en<strong>for</strong>ces certain restrictions on the scope of Variables and Procedures.<br />

A variable’s lifetime depends on how long it exists (i.e. <strong>for</strong> how long memory is allocated <strong>for</strong> the<br />

variable). The lifetime of a script-level variable extends from the time it is declared until the time the<br />

script is finished running, or until the memory is released (e.g. SET obj = Nothing statement).<br />

At procedure level, a variable exists only <strong>for</strong> as long as you are in the procedure. When the procedure<br />

exits, the variable is destroyed, and the memory previously allocated to the variable is released. Local<br />

variables are ideal as temporary storage space when a procedure is executing. Local variables with the<br />

same name can exist in several different procedures since the local variable is recognized only by the<br />

procedure in which it is declared.<br />

<strong>VBScript</strong> allows <strong>for</strong> explicit declaration of the scope of a variable through the Public or Private<br />

declarations. These declarations can also define the size of an array. The Public or Private<br />

declarations must be made at the beginning of a script, while the Dim declaration can be made at any<br />

point in the script. When using the Public or Private declarations with IWS, be sure to use them in the<br />

variable declaration section. You cannot use Public or Private declarations in IWS with Global<br />

Procedures, Command Dynamic or ActiveX events (these are sections in IWS where <strong>VBScript</strong> can be<br />

placed). Note that the use of the Public declaration of a variable may be limited by IWS, as Public<br />

variables defined in one section in an IWS application are not necessarily accessible in another section.<br />

See the <strong>VBScript</strong> Configuration and Operation in IWS section <strong>for</strong> more details on this topic.<br />

Example:<br />

Sub MySub(a,b)<br />

Dim c<br />

c =a + b<br />

End Sub<br />

Call MySub (1,2) ‘ Call the subroutine MySub<br />

MsgBox c ‘ c will be uninitialized, not the same variable as in<br />

Example:<br />

Sub Calc<br />

Dim a<br />

a = 6<br />

End Sub<br />

Dim a<br />

a = 2<br />

GoSub Calc<br />

MsgBox “a = ” & a ‘ a would equal 2, not 6<br />

Example:<br />

Private MyArray(5) ‘ Private variables<br />

Public MyVal, MyList(5) ‘ Public variables<br />

82 <strong>InduSoft</strong>, Ltd.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!