How to pass and access an object reference between classes s

Posted by Rom Elwell on 12-Dec-2012 14:20

How can I pass an object reference between classes so that all references point the same object, and not copies of the object?

For Example:
Class A wants to pass a reference to an object of type ErrorHandling to the object of type Class B, so that both Class A and Class B point to the same ErrorHandling object in memory.  This way, both Class A and Class B can update the same ErrorHandling object as necessary.

All Replies

Posted by Admin on 12-Dec-2012 14:27

Object instances in the ABL (and most other OO languages) are always passed by reference. Just define a method parameter of the ErrorHandling type and let's go!

Posted by Rom Elwell on 12-Dec-2012 14:56

Thanks Mike, you are correct and I thank you for your prompt reply.  However, I am still in the grip of a mighty brain-freeze and I'm hoping you can clarify further.  I am passing in the ErrorHandling object to a constructor of another class, and I am wondering how to reference this ErrorHandling object once it is passed in to the aforementioned constructor.

For example, consider the following three classes.  All the work is done in aClass.  It creates two objects:  objErrorHandling and objBusinessEntity.  aClass is responsible for sending objErrorHandling to objBusinessEntity so that this object can write out error states and error messages.  Ideally, I would like to pass objErrorHandling into the constructor for objBusinessEntity, then later, again in aClass, see if objBusinessEntity modified the objErrorHandling object.

CLASS aClass:

     DEF PROTECTED VAR objErrorHandling AS CLASS clsErrorHandling.

     DEF PROTECTED VAR objBusinessEntity AS CLASS clsBusinessEntity.

     objErrorHandling = NEW clsErrorHandling ( ).

     objBusinessEntity = NEW clsBusinessEntity ( objErrorHandling ).

     ...

     objBusinessEntity:DoSomeBusinessLogic ( ).

     IF objErrorHandling:ErrorState EQ TRUE THEN

          MESSAGE objErrorHandling:ErrorMsg VIEW-AS ALERT-BOX.

     ...

END CLASS.

CLASS clsErrorHandling:

     DEF PRIVATE VAR mErrorState     AS LOGICAL INIT FALSE NO-UNDO.

     DEF PRIVATE VAR mErrorMsg       AS CHAR NO-UNDO.

     CONSTRUCTOR PUBLIC clsErrorHandling ( ):

     END CONSTRUCTOR.

     DEFINE PUBLIC PROPERTY ErrorState AS LOGICAL

     GET ( ):

           RETURN mErrorState.

     END GET.

     SET (INPUT inErrorState AS LOGICAL):

          mErrorState = inErrorState.

     END SET.

     DEFINE PUBLIC PROPERTY ErrorMsg AS CHAR

     GET ( ):

          RETURN mErrorMsg.

     END GET.

     SET (INPUT inErrorMsg AS CHAR):

          mErrorMsg = mErrorMsg + '; ' + inErrorMsg.

     END SET.

END CLASS.

CLASS clsBusinessEntity:

     DEF VAR oErrorHandling AS CLASS clsErrorHandling.

     CONSTRUCTOR PUBLIC clsBusinessEntity ( INPUT inErrorHandling AS CLASS clsErrorHandling ):

           /* what should occur here?  I do not want to create a new object of clsErrorHandling.  I want to use the object that was passed as in INPUT PARAM to my constructor */

           /* oErrorHandling = NEW clsErrorHandling ( ). */     /* Doesn't this create a new instance of clsErrorHandling */

           /* oErrorHandling = inErrorHandling */

     END CONSTRUCTOR.

...

     METHOD PUBLIC VOID DoSomeBusinessLogic ( ):

          IF TRUE THEN DO:

               oErrorHandling:ErrorState = TRUE. /* what should occur here?  I do not want to use a new object of clsErrorHandling.  I want to use the object that was passed as in INPUT PARAM to my constructor */

               oErrorHandling:ErrorMsg = "A vague and not-so-informative error message".

          END.

     END METHOD.

...

END CLASS.

Posted by Admin on 12-Dec-2012 15:17

Just define a variable of type errorHalding in your business entity class - outside of the constructor so that it is accessible to all methods.  In the constructor, assign the inErrorHandling (the input parameter) to that variabel.  In later methods, reference the variable defined in step 1).

Posted by Rom Elwell on 12-Dec-2012 15:48

Thanks again Mike.  I have previously attempted this solution, unsuccessfully.  I must assume that my code is faulty and will revisit it again.  I'll post again when I have run through the regression tests with your suggested solution.

This thread is closed