| << 4.6.0- Variable Scope | Chapter4 | 4.6.2- Script Level Variables >> |
Local Variables
On the surface, it seems to defy logical explanation, but I promise you that there is logic here: let's look at it more closely. Consider two variables, both with the name strDifferent. We'll try to assign different values to these two variables:
strDifferent = "Hello I'm variable one"
strDifferent = "Hello I'm variable two"
If you returned the value of strDifferent, you'd find that is contains the string "Hello I'm variable two", thus overwriting the first value assigned to strDifferent. That's because we haven't created two variables at all; we simply created one variable, strDifferent, and then changed its value.
The exception to this rule is when we try using a similar structure in a Subprocedure. If you have a program that performs the same task repeatedly, it is sometimes more efficient to write the code that performs this task once, and access the same lines of code whenever the task is required. This is where Subprocedures come in. Subprocedures hold blocks of code that can be "called" from other parts of the program to run a specific set of actions whenever necessary. We'll discuss Subprocedures (and Functions, which are a similar concept) in detail in Chapter 5 .
If you defined strDifferent twice within two different procedures, then you'd see a different result:
Sub Procedure_Number_1
strDifferent = "Hello I'm variable one"
End Sub
Sub Procedure_Number_2
strDifferent = "Hello I'm variable two"
End Sub
If you return the value of strDifferent in Procedure_Number_1, then you'd get "Hello I'm variable one". If you return the value of strDifferent in Procedure_Number_2, you'd get "Hello I'm variable two". However, if you then go back and run Procedure_Number_1 again, you'd get "Hello I'm variable one" again. Hence, they are effectively two different variables, although they share the same name.
These variables are known as local variables (in the Microsoft documentation, they're called procedure level variables), because they are local to the procedure that created them. Outside the procedure, the local variable has no value: this is because the lifetime of the variable ends when the procedure ends. As these local variables are only in effect for a lifetime of a procedure, ASP doesn't have to worry about keeping track of them over the whole page. When a local variable is created, it only exists while the procedure that invokes it is running; once the program exits the procedure, the variable's lifetime is over, thus freeing memory and resources, increasing performance. Also you cannot create a variable twice within its scope.
Now, let's take a look at how to use two local variables. To demonstrate that they are local we will make them share the same name during our ASP program.
Try It Out – Creating Local Variables
1. Start your favorite editor and hammer out the following program:
<HTML>
<HEAD>
<TITLE>Scope</TITLE>
</HEAD>
<BODY bgcolor="white">
<%
Sub Procedure_1
strDifferent = "Hi I'm strDifferent in Procedure 1"
Response.Write strdifferent
End Sub
Sub Procedure_2
strDifferent = "Hi I'm strDifferent in Procedure 2"
Response.Write strdifferent
End Sub
%>
<P>Calling Procedure 1...<I><%Procedure_1()%></I></P>
<P>Calling Procedure 2...<I><%Procedure_2()%></I></P>
<P>Calling Procedure 1...<I><%Procedure_1()%></I></P>
</BODY>
</HTML>
2. Save the program as local.asp
3. Execute it in your browser of choice:
|
|
How It Works
This program contains two almost identical procedures:
<%
Sub Procedure_1
strDifferent = "Hi I'm strDifferent in Procedure 1"
Response.Write strdifferent
End Sub
Sub Procedure_2
strDifferent = "Hi I'm strDifferent in Procedure 2"
Response.Write strdifferent
End Sub
%>
However, each of the variables called strDifferent is defined within the confines of its respective procedure, and so doesn't exist outside the context of the procedure. If you asked the ASP program to print the value of strDifferent from outside either of these procedures, it would in fact return no value at all.
| << 4.6.0- Variable Scope | Chapter4 | 4.6.2- Script Level Variables >> |

RSS


