Shop OBEX P1 Docs P2 Docs Learn Events
Can objects be passed by reference? — Parallax Forums

Can objects be passed by reference?

AntneyAntney Posts: 6
edited 2010-04-14 02:44 in Propeller 1
Here is why I ask.·
If I create an instance of the vga_text object in the top object and then want to pass a reference to it in a child object, how would I do that? I can’t figure out how to get the runtime address of an object so I can call its methods in a child object.
Here’s a scenario that’s prompting this question.
·I am using the VGA_TEXT object to troubleshoot. I create an instance to it in the top object called “text” and call the methods text.start· and text.str, it works fine there. But when I need to troubleshoot in a lower object and reference it as text.str it won’t compile which I think is because of scoping rules. So if that’s true how do I make this work????
Anthony

Comments

  • jazzedjazzed Posts: 11,803
    edited 2010-04-14 01:16
    Objects can not be passed by reference.
    You can create a singleton version of the object by redeclaring var data as dat data.
    Then you can use the object in any file. Start the object just once in your main object.
    As I remember there is another way, but I never adopted it for some reason.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    May the road rise to meet you; may the sun shine on your back.
    May you create something useful, even if it's just a hack.
  • AntneyAntney Posts: 6
    edited 2010-04-14 01:21
    Hey jazzed

    Thanks for the reply but I am not sure I understand what you mean by redeclare var data as dat data. How does that help with object visibility? Can you provide a short example?

    Thanks

    Anthony
  • jazzedjazzed Posts: 11,803
    edited 2010-04-14 01:57
    dat variables are global. var variables are local per object.
    You will not be able to reference dat variables in another object, but the data is shared by any "declaration" of the object.
    var
      long xyz
    'becomes
    dat
      xyz long 0
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    May the road rise to meet you; may the sun shine on your back.
    May you create something useful, even if it's just a hack.
  • AntneyAntney Posts: 6
    edited 2010-04-14 02:04
    ok....I now get the whole Global vs local thanks for the clarification.

    So·but from what you are telling me I would have to go into the VGA_TEXT object and modify it so that it's a sigleton (move the VARS to DAT). And by doing that I will be able to declare it in the top object·then reference·that· object instance (which I call "text") per my example in the child object with no complie errors. Did I understand you correctly?



    Thanks

    Anthony
  • JasonDorieJasonDorie Posts: 1,930
    edited 2010-04-14 02:26
    Close - By modifying the original VGA_TEXT object to contain nothing but globals, you can now create as many "instances" of that object as you want, in as many objects as you like, without consuming additional memory. Since all of these instances of the VGA_TEXT object share the same global data, they all use the same variables, and are, in essence, the same object, even though the compiler treats them as different. Somewhat comparable to a static class in C++.

    The SPIN language doesn't handle object passing easily. There are ways you can make it happen by jumping through hoops, but it's much easier to just make a singleton version of the class you're interested in using.

    Jason
  • AntneyAntney Posts: 6
    edited 2010-04-14 02:35
    Jason

    OK now it's getting a little clearer...so·in the case of VGA_TEXT since·it calls VGA...would I also have to modify VGA?



    Thanks

    Anthony
  • jazzedjazzed Posts: 11,803
    edited 2010-04-14 02:42
    You only need to modify VGA_Text since that's where the parameter table lives.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    May the road rise to meet you; may the sun shine on your back.
    May you create something useful, even if it's just a hack.
  • AntneyAntney Posts: 6
    edited 2010-04-14 02:44
    OK Awesome...I will give it a try tomorrow...Thanks for the help!!!
Sign In or Register to comment.