Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Set Global Variable Within a Function

Reply
Thread Tools

Set Global Variable Within a Function

 
 
vunet
Guest
Posts: n/a
 
      02-17-2010
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.
 
Reply With Quote
 
 
 
 
Bob Barrows
Guest
Posts: n/a
 
      02-17-2010
vunet wrote:
> 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
....

> 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.


--
Microsoft MVP - ASP/ASP.NET - 2004-2007
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


 
Reply With Quote
 
 
 
 
vunet
Guest
Posts: n/a
 
      02-17-2010

>
> 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.
 
Reply With Quote
 
Bob Barrows
Guest
Posts: n/a
 
      02-17-2010
vunet wrote:
>> 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.


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.

--
Microsoft MVP - ASP/ASP.NET - 2004-2007
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


 
Reply With Quote
 
vunet
Guest
Posts: n/a
 
      02-17-2010
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.
 
Reply With Quote
 
Bob Barrows
Guest
Posts: n/a
 
      02-17-2010
vunet wrote:
> 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/ar...18/159378.aspx
http://blogs.msdn.com/ericlippert/ar.../20/53058.aspx


--
Microsoft MVP - ASP/ASP.NET - 2004-2007
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Set global variable from within a Class Bruce Javascript 2 10-02-2008 07:40 PM
Can we make a local variable in a function as global variable??? sairam Python 2 04-05-2007 07:16 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
function declarations, global/within a function? Douglas C Programming 2 07-05-2004 08:54 PM



Advertisments