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

  2. vunet

    Bob Barrows Guest

    Oh course it is "always True" ... you have it outside of your if statement
    ....
     
    Bob Barrows, Feb 17, 2010
    #2
    1. Advertisements

  3. vunet

    vunet Guest

    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

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

    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
     
    Bob Barrows, Feb 17, 2010
    #6
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.
Loading...