18 ObjectPAL: Custom methods and proceduresVERSIONS: All ? - written for Paradox 5
SUBJECT: Passing uiObject as var
BY: Mark Pauker
I'm aware of a few problems involving uiObjects. What I think of is related to the built-in variable known as subject. If you were to call a custom method in another form, then close the form and interrogate "subject", Paradox would crash. The reason is that when you call the method on the other form, subject changes to the object on the other form that you refer to. (Whether or not this should happen is another matter entirely!) When you close the form, subject still refers to the object on the (now closed) other form, so referencing it causes a crash.
When using a user-defined uiObject variable, if you attach it to an object on another form, close the form, and then reference it, you will get a Paradox error rather than a crash. While I'm not sure exactly why a crash occurs in one case and not in the other, my best guess is that Paradox knows to check the validity of user-defined uiObject variables every time you use them, but it _assumes_ that the built-in ones are always valid.
If we were to attach a user-defined uiObject variable to subject while subject is still valid (before the second form was closed) and then close the form and interrogate the variable, Paradox would not crash (although it would produce a runtime error). Again, this is because the new variable will be checked for validity each time before it is used.
When you pass "subject" to a method (or procedure) whose parameter is defined as const or var, you are passing a pointer to that variable (so it's as if you are using subject directly). Thus the problems will still occur. When passing it by value (without using const or var), a new variable is created which _will_ be tested for validity upon each use. (Of course, for the purposes of our discussion this is functionally equivalent to passing the variable by reference and attaching a new variable to it before the original becomes invalid.)
So to answer your question: Yes, it's possible that passing a uiObject variable using const or var could cause Paradox to crash in situations where passing it by value would not. The real question, though, is how likely is this to occur? My guess is that it's unbelievably rare, especially when you consider that (at least in the example that I gave) your program would encounter a runtime error anyway. Are you using subject anywhere? In case you are, let be get up on my soap box for a moment...
ANYONE USING 'SUBJECT' SHOULD ATTACH A UI VARIABLE TO IT IN THE BEGINNING OF THE METHOD AND REFER TO THE VARIABLE RATHER THAN TO SUBJECT. ALSO, SUBJECT SHOULD ONLY BE USED IN PROCEDURES OR LIBRARIES WHEN THE PROGRAMMER CAN GUARANTEE THAT IT WILL BE VALID WHEN THE CALL IS MADE.
Okay, I feel better now < g >.
BTW, the reason why I suggested that people consider staying away from const was because when defining a parameter as const, Paradox sometimes passes by reference and sometimes passes by value. I thought that the side-effects of this could be confusing, especially to beginners. Other than that, passing parameters using const is a perfectly reasonable thing to do, and I do it all the time in non-recursive methods.