Set Global Variable Within a Function

V

vunet

I'd like to set a global variable within a function not to set it
every time when needed. However, this never works for me using Is
Nothing check like so:

set objXML = nothing
function setAppXML ()
response.write "<br>objXML="&(objXML is nothing)&"<br>" <--always
True <------------
if objXML is nothing then
Set objXML = Server.CreateObject("Microsoft.XMLDOM")
objXML.async = False
setAppXML = objXML.Load (Server.MapPath("test.xml"))
else
setAppXML = true
end if
end function

Please recommend a solution or advise. Thanks.
 
B

Bob Barrows

vunet said:
I'd like to set a global variable within a function not to set it
every time when needed. However, this never works for me using Is
Nothing check like so:

set objXML = nothing
function setAppXML ()
response.write "<br>objXML="&(objXML is nothing)&"<br>" <--always
True <------------

Oh course it is "always True" ... you have it outside of your if statement
....
 
V

vunet

Oh course it is "always True" ... you have it outside of your if statement
...

Sorry I was not clear. The objXML is declared outside of function to
be global. I call setAppXML many times on the page below so I would
not have to set the object (i.e. load XML) every time I call the
function. So once it is set, it should be FALSE to Is Nothing but it
is always TRUE.
 
B

Bob Barrows

vunet said:
Sorry I was not clear. The objXML is declared outside of function to
be global. I call setAppXML many times on the page below so I would
not have to set the object (i.e. load XML) every time I call the
function. So once it is set, it should be FALSE to Is Nothing but it
is always TRUE.

You're going to have to show us how to really reproduce your problem. The
code you showed us will always result in the message
objXML is nothing

Create a new page, include nothing but the code that deals with objXML, make
sure it reproduces your problem, then post it here.
 
V

vunet

I discovered the problem. I was setting it to Nothing at the end of
another function. Sorry for confusion.
However, while I was googling I found a statement where it says that
global variables should not be declared within a function in VBScript.
It's a bad thing to do and against the rules.
Is it really true?
Many thanks.
 
B

Bob Barrows

vunet said:
I discovered the problem. I was setting it to Nothing at the end of
another function. Sorry for confusion.
However, while I was googling I found a statement where it says that
global variables should not be declared within a function in VBScript.
It's a bad thing to do and against the rules.
Is it really true?
Many thanks.

Well, it's kind of a contradiction in terms. Declaring a variable is done
via a Dim statement:

Dim myvar

Declaring it outside of a procedure gives it "global" scope. Declaring it
withing a procedure limits its scope to that procedure. So, the statement
you are describing would be more correct if it said "it is not possible to
declare global variables within a function", and the language is irrelevant.

I believe your misunderstanding stems from incorrectly applying the term
"declare" to the Set statement, which assigns an object to a variable.

That said, in vbscript, there is no need to explicitly declare variables.
The first reference to a variable essentially declares it. So I can see
where you might make the mistake of thinking the Set statement declares the
variable.

Anyways, to avoid confusion, you should always use OPTION EXPLICIT at the
beginning of your pages, which forces you to explicitly declare your
variables. It will become quickly obvious that global variables cannot be
declared within procedures (subs or functions).

Here is some fun reading:
http://blogs.msdn.com/ericlippert/archive/2004/06/18/159378.aspx
http://blogs.msdn.com/ericlippert/archive/2003/09/20/53058.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top