Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
NewObject or CreateObject or AddObject
Message
From
28/08/2007 10:23:00
 
 
To
28/08/2007 09:38:35
Jay Johengen
Altamahaw-Ossipee, North Carolina, United States
General information
Forum:
Visual FoxPro
Category:
Object Oriented Programming
Environment versions
Visual FoxPro:
VFP 8 SP1
Miscellaneous
Thread ID:
01250745
Message ID:
01250904
Views:
21
This message has been marked as a message which has helped to the initial question of the thread.
Good information, thanks! I still can't understand why, when I create an object while in another object, the child does not have a parent. That goes against logic to me, but not knowing the rules, maybe it doesn't.

I am not quite sure what you mean by "while in another object". It does not matter where you create an object, but how.

Aggregation is a technique used to manage the interaction between objects by having one object create and hold a reference to another. You are using aggregation whenever you use commands like this:
This.oRef = CREATEOBJECT( ‘MyClass’ )
or this
This.oRef = NEWOBJECT( ‘MyClass’, ‘MyLibrary.vcx’ ).
In many languages, C++ for example, you have no choice but to use aggregation when constructing classes from multiple components. Unlike Visual FoxPro, these languages have no concept of true containership.

Composition depends upon one object owning another and is the basis of containership. Composition is the process by which classes consisting of a container and one or more controls based on other classes are created, whether at design time or at run time. At design time, it is very easy to define composite classes in the visual class designer. This is what we are doing every time we drop a control on a page in a page frame. The page “owns” the controls that it contains. We are also using composition whenever we use:
This.AddObject( ‘oMyObject’, ‘MyClass’ ).
The key difference between the mechanisms for composition and aggregation is that for aggregation, the property must be defined in advance and an object reference assigned to it. In composition an instance of the object is created, with the specified reference, as a child of the parent container. While the end result of both composition and aggregation is, apparently the same, that does not alter the fact that the key element in composition is that there is a parent-child relationship between the container itself and the objects which it contains.

This has one very significant benefit for us as developers. Since there is a parent-child relationship Visual FoxPro can manage the garbage collection for us. By definition, an object which is contained, and owned, by another shares the scope of its parent. So when you destroy a composite object, all of the components are released also (Forms could not work if this were not the case!). This is not the case when you use aggregation, you must be especially careful to clean up any outstanding object references because VFP cannot resolve them for you.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform