The usage scope of variables in ASP

Source: Internet
Author: User
Tags copy end variables variable valid
The usage scope of variables in ASP

<%
Dim a
A = 10

function AA ()
Dim b
b = 5
Response.Write (a) ' output here is 10, the variables defined outside the function can be called global variables, valid at any location, including function interior, sub internal, class internal
A = 5 ' because A is defined outside AA, the change to a here will affect the value of global a
End Function

Call AA ()

Response.Write (a) ' Here the output is 5, because a is changed in the AA function.
Response.Write (b) ' Here the output is null, because B is defined within the function AA, so B is only valid within AA
%>
Look at one more example
<%
Dim a
A = 10
function AA ()
Dim a ' note that here a dim a is added to the function above, the variable declared within AA is valid only within AA, and a in this function has nothing to do with a on the outside of the function, so he does not affect the value of a outside of the function.
A = 5
End Function

Call AA

Response.Write (a) ' This output is 10, because the variable declared inside the function is only valid internally
%>

Third case
<%
Dim a
A = 10

' Note that the parameters here Use ByVal, without ByVal By default, ASP will use ByRef to pass the value, ByVal simple can be understood as a copy of the copy parameter, so the change of a in AA is actually just a copy of a change, does not affect the value of the external A, So the output a at the back is still 10.
Function AA (ByVal a)
A = 5
End Function

Call AA (a)

Response. Write (a)
%>


Fourth case
<%
Dim a
A = 10

' The way the value is passed is changed to byref, and the effect of no byref is not the same, the function in VBScript and the default method of passing values are byref
' ByRef is the actual memory address that transmits the parameter, so modifying a within a function is equivalent to modifying the value of the outer a of the function, so the following will output 5
function AA (ByRef a)
A = 5
End Function

Call AA (a)

Response. Write (a) ' The output here is 5 because the value of a in function AA is changed
%>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.