Wiki Home


Namespace: WIN_COM_API
The ADDOBJECT method is normally used to add other objects to a container class at runtime. The object that is added will have its Parent property set to the container object. The lifetime of the object is tied to the lifetime of the parent. Any object added via ADDOBJECT will destruct automatically when the container object destructs. You can use the Remove Object method to get rid of contained objects at runtime, if you want to get rid of contained objects before they'd normally destruct.

In this respect ADDOBJECT is different from Create Object which also instantiates objects but doesn't put the object within the containership hierarchy. Typically you'd hold a reference to these objects in an object property or a memvar. For any objects that are held in an object property you should explicitly set the property to .NULL. to delete the object.

this.AddObject( "oContainedObject", "SomeClass" )
? this.oContainedObject.Parent.Name
this.RemoveObject( "oContainedObject" ) && get rid of it

this.oAssociatedObject = CreateObject( "SomeClass" )
? type( "this.oAssociatedObject.parent" ) && = U
this.oAssociatedObject = .null. && get rid of it

The method can also be called at design time. Here's a Command Window technique to change the control in a column of a grid.
There is another significant difference between objects instantiated with ADDOBJECT and Create Object. ADDOBJECT objects fully participate in the event loop of the container, Create Object objects do not participate in the event loop. For example on a Form a Create Object object can not be made visible and doesn't get Refresh messages.
Some significant notes.

In run-time, before object adding you require to make it visible. All controls added in run-time are invisible from start. When use such invisible controls in the grid column, these controls appears just as disapled.

When adding new header to column, old header removed automatically. So don't use Remove Object for header in column when you replace it. Note also that you cannot remove header from column at all, VFP will maintain it at all cost by placing default header.

When removing object, always make it invisible before that. This to purge extra refreshing object events. Without this application will work slower, memory might contain garbage. In older versions of VFP (at least, VFP 5.0) you will also not be able to create object with the same name just after removing old object when not make it invisible. This is very often situation for grids added programmatically - always make the invisible before removing.

-- Vlad Grynchyshyn
See also Container Classes Create Object
Category Class Design Category VFP Methods
( Topic last updated: 2001.02.06 04:44:11 AM )