Shop OBEX P1 Docs P2 Docs Learn Events
Calling a method in another object — Parallax Forums

Calling a method in another object

EnriqueEnrique Posts: 90
edited 2008-12-01 18:37 in Propeller 1
Hi,
·
Say you have three objects: one we will call Top, which will instantiate objects A and B. Can the top object call a method in B and then can this method in B call a method in A?
·
Example:
·
{{
File Top.spin
}}
obj
  OA:"A"
  OB:"B"
pub main
  OB.MethodB
  

{{
File A.spin
}}
pub MethodA
' does something


{{
File: B.spin
}}
pub MethodB
' does something
' somehow calls MethodA in object OA


Thanks,

Enrique

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-11-30 14:52
    No

    The only way to call a method in object A from object B is to have a declaration of the object in B (like OBJ OA:"A"). If object A has its own VAR variables, these will be duplicated in each copy (instance) of object A. There are some objects in the Object Exchange that only have variables in a DAT section and this is shared (common) among all copies of the object. Here's one example: obex.parallax.com/objects/189/.
  • EnriqueEnrique Posts: 90
    edited 2008-11-30 15:44
    I thought so, I was wondering whether there was a way to pass the reference of one object to another one.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-11-30 16:20
    No, not really. There are ways to fiddle with the internal representation of objects at run-time to substitute one method or set of methods for another, but it's not something to use for anything but experimentation by someone who really wants to learn how Spin works internally.
  • EnriqueEnrique Posts: 90
    edited 2008-11-30 16:29
    Thanks
  • Ken PetersonKen Peterson Posts: 806
    edited 2008-12-01 17:42
    Enrique:

    If your A object is a "singleton", then you can instantiate the A object within the B object as well as the Top object and they will both refer to the same object. Then you can call methods on A from both Top and B.

    To make an object a "singleton", put all of the variables in that object in the DAT section instead of the VAR section. When an object is instantiated at more than one place in a program, only one copy of the code and the DAT section are used, but the compiler will make a copy of the VAR section for each instance of the object. If the variables are all in DAT, then all instances of the object are the same.

    I have done this with the FullDuplexSerial object because I wanted to share the same serial interface at different levels within the program. I've also done it with the TV text object so I could share the display with several different objects.

    Keep in mind that the object has to be coded to allow this. If you are using someone else's object, you may have to make a modifications to it to make it a singleton. Also, if the object has a start() or init() routine, that should only be called once, i.e., you wouldn't call start() from both Top and B.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"I have always wished that my computer would be as easy to use as my telephone.· My wish has come true.· I no longer know how to use my telephone."

    - Bjarne Stroustrup
  • EnriqueEnrique Posts: 90
    edited 2008-12-01 17:56
    In my case I instantiate OA 4 times.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-12-01 18:10
    Ken Peterson said...
    Also, if the object has a start() or init() routine, that should only be called once, i.e., you wouldn't call start() from both Top and B.
    A well-written object should be able to tell whether it's already been started and inhibit multiple starts, assuming that that's the behavior you want. This relieves the programmer who uses the object from having to apply knowledge across object boundaries about what other objects are doing. By letting each object call start(), even if some of thoses calls are redundant, you improve object encapsulation.

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Just a few PropSTICK Kit bare PCBs left!
  • mparkmpark Posts: 1,305
    edited 2008-12-01 18:18
    My Homespun compiler has object-passing capability of a sort, that sort being rudimentary and experimental.
  • Ken PetersonKen Peterson Posts: 806
    edited 2008-12-01 18:37
    Good advice, Phil. I have used Singleton objects in other languages (particularly Java) and keeping track of whether an instance already exists is key to defining class with the Singleton pattern (as well as making the constructor protected). I was just trying to cover the basic idea here in the forum.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"I have always wished that my computer would be as easy to use as my telephone.· My wish has come true.· I no longer know how to use my telephone."

    - Bjarne Stroustrup

    Post Edited (Ken Peterson) : 12/1/2008 6:43:54 PM GMT
Sign In or Register to comment.