OOP and Shared Vars

Hi, All!
I defined shared vars in a class, then system showed error message "SHARED entities may not be defined in a Class or Interface." Can I create shared variable in the class?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

RE: OOP and Shared Vars

One way of achieving this is having a class wrap a persistent procedure where the shared variables are defined. Have a property defined in your class for each shared variable and then use getter and setter functions to read and write the shared variables in the persistent procedure:



SharedVariables.cls:

CLASS SharedVariables:

  DEFINE PRIVATE VARIABLE hData AS HANDLE NO-UNDO.

  CONSTRUCTOR SharedVariables():

    /* Start the procedure that defines the shared variables */
    RUN SharedVariablesData.p PERSISTENT SET hData.

  END CONSTRUCTOR.

  DESTRUCTOR SharedVariables():

    /* Clean up persistent procedure */
    DELETE PROCEDURE hData NO-ERROR.

  END DESTRUCTOR.

  /* Function Prototypes map to functions in the persistent procedure */
  FUNCTION GetMyVariable RETURNS CHARACTER
    MAP TO GetMyVariable IN hData.
  FUNCTION SetMyVariable RETURNS LOGICAL(INPUT myVar AS CHARACTER)
    MAP TO SetMyVariable IN hData.

  /* Property calls the function prototypes */
  DEFINE PUBLIC PROPERTY MyVariable AS CHARACTER NO-UNDO
    GET:
      RETURN GetMyVariable().
    END GET.
    SET(INPUT val AS CHARACTER):
      SetMyVariable(val).
    END SET.

END CLASS.




SharedVariablesData.p:

DEFINE NEW SHARED VARIABLE MyVariable AS CHARACTER NO-UNDO.

/* Functions */
FUNCTION GetMyVariable RETURNS CHARACTER:
  RETURN MyVariable.
END FUNCTION.

FUNCTION SetMyVariable RETURNS LOGICAL (INPUT myVar AS CHARACTER):
  MyVariable = myVar.
  RETURN TRUE.
END FUNCTION.




Example Usage:

DEFINE VARIABLE vars AS SharedVariables NO-UNDO.

vars = NEW SharedVariables().

vars:MyVariable = "Hello World".

MESSAGE vars:MyVariable VIEW-AS ALERT-BOX.


tamhas's picture

This strikes me as upside

This strikes me as upside down. The goal should be in the end to get rid of the shared variables, not to enshrine them as a part of the class. What is the function of shared variables anyway but a poor man's parameter dating from a time when we had no parameters. Every site still using shared variables should be working to convert shared variable references into parameters so that the interface signature is clear and the direction unambiguous. So, one can't eliminate them, at least confine them to a wrapper calling procedure which then passes the values to and from the object via the constructor or methods. That way, the object is in its final form and one can do away with the wrapper when one finally gets rid of the shared variables.


tamhas's picture

NO, the manual is quite

NO, the manual is quite explicit about saying that defining shared variables is not legal in a class. It would be an abomination from an OO point of view. If you need to interface with legacy procedural code that uses shared variables, create an interface procedure to turn the shared variables into something OO-like ... or, better yet, refactor the original to get rid of the shared variables.