To illustrate the "every object lives in one session" stetement:
Code:
oF1 = CreateObject("myform")
oF2 = CreateObject("myform")
oF1.addchild("child1","Anything")
oF2.addchild("child1","Anything")
oD0 = CreateObject("Anything")
oD1 = oF1.createdetachedobject("Anything")
oD2 = oF2.createdetachedobject("Anything")
* expected output 1,1,2,2,2,3,3,3 (unless you start with multiple datasessions already)
* everything in the default (1) datasession
? Set("Datasession")
? oD0.ShowOwnDatasession()
* oD0, created in the main code, is also part of the default datasession ID 1)
* everything in the same session as form1 (private Datasession ID 2)
? oF1.datasessionid
? oF1.child1.ShowOwnDatasession()
? oD1.ShowOwnDatasession()
* everything in the same session as form2 (private Datasession ID 3)
? oF2.datasessionid
? oF2.child1.ShowOwnDatasession()
? oD2.ShowOwnDatasession()
* oD1 and oD2 are no children objects of form1, still are created in the same data session id
Define Class myform as Form
datasession = 2 &&private
Procedure addchild(name,class)
This.AddObject(name,class)
EndProc
Procedure createdetachedobject(class)
Return CreateObject(class)
EndProc
Enddefine
Define Class Anything as Custom
Procedure ShowOwnDatasession
Return Set("Datasession") && show current (own) datasessionid
Endproc
Enddefine
You can see it depends whether you run Createobject inside form1, form2 or from outside, which datasession it the created object belongs to, not only for objects, that are actual children of the forms (and have the form object as their parent property).
It all boils down to this, though: If you don't do anything special, every object falls into the right place (i.e. datasession) and therer are no surprises. that controls don't have access to the forms workareas. The way it's automatically working is so well designed, that you have to dig deep to break something on some other objects private datasession.
If you do things like creating a factory class by reading about design patterns (like the mother of all books about that by the gang of four) and your factory class is living in one session, be it datasession id 1 or any private session, you suddenly get struck by all your class instances living in that same datasession, whether they should or not. And that means it's no good idea to reinvent the wheel by a factory that you make the only point of all object creation, there are the several object creating functions and methods and you just need to know which of them to use for which objects. On the other hand knowing all that you can take advantage of a factory that knows about where which classes are defined and how to create them with which requirements to be fullfilled if you program it in a way that it delegates the actual object creation to whichever class should actually do it. And then also don't exaggerate by letting the factory create controls of forms. by interpreting the factory pattern principle very strict. You simply design forms with controls and they are therefore part of the form and there's no need for a factory controlling the controls creation, monitoring or overlooking it as a god (manager) of all objects created. Also see discussion about god classes, for example as simple as by Wikipedia.
So in that respect (design patterns): It's good to know them, but also know VFP in and out to know how to apply the patterns within the VFP language. I'm not saying there are no pros in creating something like abstract classes of each type of design pattern to have a concrete implementations of the pattern and a class that overlooks and enforces the rules of its own pattern. But it has to be done in a way the VFP language allows it with its design, no matter if you call datasession dependency or membership a design flaw or not, for example.
And sorry, you didn't mention patterns, a factory or anything like that, i's just a good opportunity to talk about this. I have the feeling your question about determining which form has a sepcific datasession is about wanting to switch to it and influence it from outside, design a god''/manager class that can do so and by that break some of the principle of OOP, no matter if you think of core OOP principles or in terms of design patterns and espcially the factory pattern.