Set Global Variable Within a Function

Discussion in 'ASP General' started by vunet, Feb 17, 2010.

  1. vunet

    vunet Guest

    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.
     
    vunet, Feb 17, 2010
    #1
    1. Advertising

  2. vunet

    Bob Barrows Guest

    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"
     
    Bob Barrows, Feb 17, 2010
    #2
    1. Advertising

  3. vunet

    vunet Guest


    >
    > 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.
     
    vunet, Feb 17, 2010
    #3
  4. vunet

    Bob Barrows Guest

    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"
     
    Bob Barrows, Feb 17, 2010
    #4
  5. vunet

    vunet Guest

    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.
     
    vunet, Feb 17, 2010
    #5
  6. vunet

    Bob Barrows Guest

    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/archive/2004/06/18/159378.aspx
    http://blogs.msdn.com/ericlippert/archive/2003/09/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"
     
    Bob Barrows, Feb 17, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bryan Parkoff
    Replies:
    2
    Views:
    6,577
  2. Replies:
    1
    Views:
    406
    Michael Ekstrand
    Aug 21, 2005
  3. Douglas
    Replies:
    2
    Views:
    364
    Herbert Rosenau
    Jul 5, 2004
  4. sairam
    Replies:
    2
    Views:
    384
    Steve Holden
    Apr 5, 2007
  5. Bruce
    Replies:
    2
    Views:
    98
    Jeremy J Starcher
    Oct 2, 2008
Loading...

Share This Page