1

This might be an elementary question... But its got me stumped.

At the beginning of an ASP/VBscript page, I am calling a sub() to get some config values of the application. I am using a sub so that I do not need to write the same query on each page of the application to get these values. This sub is included within a file which is being #included at the top of each page.

I have tested and have confirmed that within the sub() the config variables have correct and valid values. But when I try call these variable values later in the page, they are empty.

Is there some kind of permission setting on the sub to make the values accessible throughout the calling page?

4
  • 2
    It will be variable scope, use Option Explicit and Dim to make sure that all your variables are declared and scoped correctly. What I mean by scope is for example if you have have variables declared using Dim inside a Sub procedure they will only be available inside that procedure. To make them available outside the procedure declared them outside using Dim. This is also Unknown as globally defining variables. Take a look here for my information on variable declaration and scope. Commented May 22, 2016 at 16:25
  • 1
    thank you for your patient answer! Commented May 22, 2016 at 21:19
  • 1
    @Lankymart - if you will, enter your response as the answer, i will check it off. Commented May 23, 2016 at 12:31
  • 1
    Such values are usually stored as Application Variables, which get global scope by their very definition. Commented May 23, 2016 at 13:02

1 Answer 1

2

The issue here will be the scope of the variable.

Take this example, we have a one variable called testvar but it's defined in two different scopes, the first is in the Global Scope and the second is in the Local Scope of a Sub Procedure.

Option Explicit
'Variable declared in the global scope will be available to any procedure
'wishing to use it.
Dim testvar: testvar = "global"

Call Test()
Call Test2()
Call Response.Write(testvar & "<br />") 'Will equal "global changed"

Sub Test()
  Call Response.Write(testvar & "<br />") 'Will equal "global"
  'Updating the global variable.
  testvar = "global changed"
End Sub

Sub Test2()
  'Variable declared in the local scope and will only be available
  'to the procedure it is declared in.
  Dim testvar: testvar = "local"
  Call Response.Write(testvar & "<br />") 'Will equal "local"
End Sub

Output:

global
local
global changed

Where this could get confusing is when the testvar declarations are not defined correctly causing ambiguity.

Just remember variables declared inside a Sub / Function are only available to that Sub / Function and cannot be used outside the scope of that Sub / Function.

Variables declared outside of a Sub / Function also known as Global Scope (not always the case if we start talking about Classes, but lets keep it simple for now) are available to any Sub / Function and can be called anywhere in a page (including from #include as this is just the extension of the existing pages code).


Useful Links

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.