Accessing global/shared variables inside a class

Hello!

I try to refactor an existing source i.e. make a class out of it.
The problem is that the existing source accesses (and changes) global shared variables.

When I create a class and want to reuse the existing code I get a problem because shared entities cannot be defined inside a class or interface. How can I access and change these global shared variables inside my class?

(One solution would be to split up a big method into smaller ones and change the global shared vars outside the class in the class-creating procedure-file, but I want to know how this can be done within a class).

Thanks in advance
Harry


Comment viewing options

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

found something...

hello,

it seems that the global variables must be accessable via get/set-methods inside a p-file, so that the classinstance can access them via DYNAMIC-FUNCTION.

Sample:
CLASS Person:
DEFINE VARIABLE hAddress AS HANDLE NO-UNDO.

CONSTRUCTOR PUBLIC Person():
RUN Address.p PERSISTENT SET hAddress.
END CONSTRUCTOR.

METHOD PUBLIC VOID processAddressID():
DEFINE VARIABLE addressId AS CHARACTER NO-UNDO.
DYNAMIC-FUNCTION("setAddressId IN hAddress", addressId).
addressId = DYNAMIC-FUNCTION("getAddressId IN hAddress").
END METHOD.

DESTRUCTOR PUBLIC Person():
DELETE PROCEDURE hAddress.
END DESTRUCTOR.
END CLASS.

If someone has got a better idea - please let me know.
Greetings from Austria
Harry


tamhas's picture

Go all the way

Perhaps this is more than you want to do at this point, but my recommendation is to start your refactoring with the global shared variables. Create a singleton (see http://www.oehive.org/PseudoSingleton ) to hold the values now associated with the global shared variables and populate both the variables and the properties of the singleton. Then, in new code, use the singleton while the old code continues to use the shared variables. As opportunity presents, rip out references to the shared variables and replace them with references to the singleton.