Passing "handle" for variable to low-level functions
2 views (last 30 days)
So a very similar question was asked here in 2012, and the answer was 'no', but I wanted to find out if there was any changes/alternative techniques.
The TLDR is that I want a low-level class method to access data in my upper level workspace without
(a) duplicating the data (memory issue)
(b) having to pass the data as an input argument at every single level of a function tree (programming headache)
(c) GLOBALS (yuck)
I want to be able to save a handle to that upper level workspace variable in the properties of my class, so that it can be accessed by sub-functions/methods.
Let's say I have an existing, well developed class myClass. It has lots of methods (myFun1,myFun2...) and sub-functions (mySubFun1,...) that may be called by those methods. Now, I add some new sub-function myNewSubFun that requires information not stored in the existing properties, but instead is in the high level workspace and has been created seperately. myNewSubFun may also now be called by any of the existing methods (myFun1, etc.) depending on chosen parameters.
Let us put that one piece of requred information buried deep inside of myExternalObj, which is a member of its own class and is sitting in the same workspace as an instance of myClass. That class has some method val = getVal(myExternalObj) that returns the value I want. In fact, myExternalObj may be any of a number of classes, each of which has their own getVal implementation. Let us also say thay myExternalObj has a large memory footprint, and I don't want to duplicate it.
Normally, I would follow one of three courses, two of which I reject here;
(A) Use getVal at a high level, and put it in the properties of myExternalObj. I don't want to do this, because there may be parameters determined in myNewSubFun that actually effect what I want getVal to return. (toy example: IN myNewSubFun -- if moveMethod = swim, speed = getVal('swimming'), else, speed = getVal('walking')).
(B) Pass myExternalObj down the function tree to myNewSubFun. The problem is that this forces me to add an entry for myExternalObj into the input arguments of every method and sub-function that may eventually call myNewSubFun. This is both labor intensive, and bug-prone.
(C) Put myExternalObj into one of the properties of myClass. This may be the way to go (and I could put it in as a transient property with SetAccess = private, so it never saves).
Option (C) is the closest to what I want; provided I do not modify either myExternalObj in the upper workspace, or myExternalObj the property, they both occupy the same memory real estate. If I am careful, this will work...but, what if I want to be able to modify the version in the upper workspace without re-loading it into the properties of myClass?
In my mind, the best way would be to have a handle to myExternalObj that gets saved in the properties of myClass. Then, when myNewSubFun needs access, it uses the handle rather than looking for that object being passed to it. This is easy if myExternalObj is a subclass of handle, but it is emphatically not.
So, 3 questions
(1) Would using " (SetAccess = private)" when using option (c) absolutely prevent a second copy of myExternalObj from being created in memory, provided that I did not modify the original?
(2) Is there any way to pass a handle for a data object in a high level workspace to a function in a low level workspace?
(3) Is there an obvious 3rd way, or "I can't believe you are even trying this"?
Side note...I somehow changed fonts and do not know how to swap back...