How can I pass an object reference between classes so that all references point the same object, and not copies of the object?
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.
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!
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.
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.
DEF PRIVATE VAR mErrorState AS LOGICAL INIT FALSE NO-UNDO.
DEF PRIVATE VAR mErrorMsg AS CHAR NO-UNDO.
CONSTRUCTOR PUBLIC clsErrorHandling ( ):
DEFINE PUBLIC PROPERTY ErrorState AS LOGICAL
GET ( ):
SET (INPUT inErrorState AS LOGICAL):
mErrorState = inErrorState.
DEFINE PUBLIC PROPERTY ErrorMsg AS CHAR
GET ( ):
SET (INPUT inErrorMsg AS CHAR):
mErrorMsg = mErrorMsg + '; ' + inErrorMsg.
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 */
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".
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).
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.