Forum Discussion

jbrave's avatar
jbrave
Channel Surfer
15 years ago

Global variable contextuality

if in main() I create an object:



obj=GetObject()

function Getobject() as object
return
{action:functionname
action2:function2name
variable:"value"}
end function


it seems that I have to access it via its name:

?obj.value

instead of m.obj, in the main function, but in subroutines or functions called from main, it is available as a global:

sub printvalue()
?m.value
end sub

further, what if I create a global first:

m.test="xyz"

and then create obj

obj=getobject()

It seems that in some circumstances m.test will be available after m.obj is created and in other cirumstances, it seems to disappear. I first encountered this issue when working on the update to a client project that was based on the Deviant ART example project - most of the globals come into existance when

rss=CreateMediaRSSConnection()

I found that in some cases I could not create new globals that would be added to the combined m. list of globals, so I wound up passing lots of information back and forth through various functions, not that there is anything wrong with that. I"m thinking that persistant globals are created at the top level and globals created in subroutines are non-global if a master object like the obj described at the top of this post has been created, but are still global for subs and functions called from the subs and functions where the global was created. That might be a tough sentence to chew on, sorry.

Comments?

- Joel

3 Replies

  • "m" is only the global object if the function is NOT used as a method.


    function initTest()
    m.foo = "bar"
    end function

    function returnFoo() as object
    return m.foo
    end function

    function makeObject() as object
    return { method: returnFoo }
    end function

    sub main()
    initTest() ' sets global m.foo
    print returnFoo() ' returns m.foo
    obj = makeObject()
    print obj.foo ' prints invalid
    print obj.method() ' prints invalid
    obj.foo = "baz" ' set object's .foo member variable
    print obj.method() ' prints baz
    print returnFoo() ' prints bar
    end sub

  • jbrave's avatar
    jbrave
    Channel Surfer
    So is this:


    obj=GetObject()

    function Getobject() as object
    return     
          {action:functionname
           action2:function2name
           variable:"value"}
    end function


    Being used as a method? Can you elaborate a little bit?

    I
  • "jbrave" wrote:
    So is this:


    obj=GetObject()

    function Getobject() as object
    return     
          {action:functionname
           action2:function2name
           variable:"value"}
    end function


    Being used as a method? Can you elaborate a little bit?


    I would say that GetObject is a function that generates an object.

    Calling obj.action() would be using the "action" method of the "obj" object that the GetObject() function created. I use method to refer to a function that operates within the context of an object, and function to refer to a stand-alone function.

    Calling functionname() would be calling the same bit of code as obj.action() above, but the "m" variable would refer to something different (the global m object as opposed to the object's data).

    In summary, functions can use m and access a global data store, or "global object" if you prefer, while methods can use m to access the object's data. Currently you can't use a function as a method (assign to a object variable) and have it access the global data though m.

    If you really want globals within methods, create yourself some helper functions like so:

    function setGlobal(name as string, value as dynamic)
    m[name] = value
    end function

    function getGlobal(name as string) as dynamic
    return m[name]
    end function


    and use those to set/get global values. It's also far less ambiguous what you are accessing, so there won't be any confusion between accessing object data and global data.

    P.S. I haven't actually bothered to use the above in production, but barring any simple typo problems, I think they should work.