Shop OBEX P1 Docs P2 Docs Learn Events
Why doesn't this work? Passing an object symbol — Parallax Forums

Why doesn't this work? Passing an object symbol

CJMJCJMJ Posts: 226
edited 2021-02-03 18:17 in General Discussion
OBJ
  objA : "object_name"
  objB : "object_name"

PUB go()

  private(objA)
  'private(@objA)  -- doesn't work
  'private(@@objA) -- doesn't work

PRI private(object)

I keep getting this error message (the error pointer is right at the end of objA):

292 x 268 - 7K

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2021-02-04 03:47

    The code from the child object is compiled with the parent object so the name of the object is not saved to RAM. The object name is just used by the compiler.

    I use a "programName" string at the top of many of my Spin and Spin2 programs.

    DAT programName byte "_ZOledAttempt210131a.spin2", 0

    I add an "objectName" to a DAT section in many of my objects.

    DAT objectName byte "OledBootFlash210203b.spin2", 0

    Place the strings "programName" and "objectName" on line #1 of both objects. This makes it easier for me to update them with a date code.

    I include the following method in the child object so the parent object can access the child object's name.

    PUB GetObjectName() : result
    
      result := @objectName
    
    

    Here's an example of how these strings are used.

    OBJ
    
      Usb : "jm_fullduplexserial"                          { Uses one cog.} 
      Flash : "OledBootFlash210203b"                             { Uses one cog.}
    
    PUB DisplayMenuHeading()
    
      Usb.Tx(Usb.CLR_EOL){ These three commands should clear the bottom of menu.}
      Usb.Tx(Usb.CR)
      Usb.Tx(Usb.CLR_DN)
    
      Usb.Tx(Usb.HOME) { Start menu at top.}
    
      {Start by identifying which program is running.}
      Usb.Str(string(11, 13, "programName = "))
      Usb.Str(@programName)
      Usb.Str(string(11, 13, "flash object name = "))
      Usb.Str(Flash.GetObjectName())
    

    Edit: I had to change the way comments were written in order to get the code blocks to work.

  • Ah, I was under the mistaken impression that these "children" object files were treated like real objects/structures, like in C++, where each instantiation was assigned an address and their methods and attributes were offsets from that address.

    My understanding was that the CONSTANTS for a "child" object were compiled into one location and all the instances of that object used the same address of that CONSTANT. I was trying to eliminate redundant code by using the CONSTANT at a CLASS level. I could have just accessed the CONSTANT from one of the instances but that looks weird.

    What I ended up doing was creating CONSTANTS in the parent object that referenced the CONSTANTS in one of the instances. It doesn't look as weird using it that way.

    Thanks, @"Duane Degn"

    I do something very similar to what you illustrated above for storing versioning information and menu items within each child. The parent than makes calls to the child so it can build a master menu of available functions.

  • FWIW I often have a "shared constants" ojbect which is included in multiple child objects.
    Constants don't increase the size of a program but the Propeller Tool has a limit to how many can be created. I have run out of constants many times.

Sign In or Register to comment.