Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

Parent

This keyword has nothing to do with family trees, either yours or an object’s. Parent points to the container object containing the specified object. It traces through the containership hierarchy, telling you which object contains the current object. Don’t confuse it with ParentClass, which tells about the class hierarchy.

Usage

oParentObject = oObject.Parent
uValue = oObject.Parent.pProperty
oObject.Parent.pProperty = uValue
oObject.Parent.mMethod [ ( uParams ) ]

Some classes of objects are allowed to contain other objects. For example, forms contain controls, grids contain columns, and so forth. If an object is contained in another object, Parent gives you a reference to the containing object.

Parent is handy when you want to reference properties and methods of other, related, objects. For example, in a grid column, you might want to refer to a property of the grid. To do so, you can use This.Parent.<whatever>. You can even carry this further and walk down another part of the family tree (to a sibling, niece or nephew of the current object) with a notation like This.Parent.Column2.<whatever>.

Some controls may be buried several layers down in the object hierarchy. For example, consider a check box that’s in a column of a grid, which in turn is on a page of a page frame. The check box’s parent is the column. The column’s parent is the grid. The grid’s parent is the page. The page’s parent is the page frame. Then, finally, the page frame’s parent is the form.

Given the possibility of so many layers of nesting, and that you may not know how many layers are involved, in many cases it’s better to use ThisForm or ThisFormSet when you can. The choice of whether to trace up and back down the hierarchy or just jump to the top and back in really depends on how closely related the objects are and how much you know about the setup. For example, to move from a control in one column of a grid to the control in another column of the same grid, it’s probably best to use something like This.Parent.Parent.Column2.TheControl. But, to go from a control in a column of a grid to another control elsewhere on the form, ThisForm.OtherControl seems simpler and avoids the question of whether the grid is within another grid, or in a page frame, or anything else like that.

You can’t change the Parent of an object; you can simply look it up and use it to refer to other objects.

We can’t figure out whether Parent is actually a property of every base class or just an object reference keyword like This. In VFP 3, checking AMEMBERS() for an object doesn’t turn up Parent, but starting in VFP 5, it does. For that matter, Parent shows up in the debugger looking like a property. Objects created with SCATTER NAME don’t have a Parent property, but then they don’t have a Name property, either, so we’re not sure we want to judge anything by them. We’re not sure it really matters, but it would be nice to know for sure. This week, we’re inclined to lean toward Parent being a property.

Example

* Use the SetAll method of a commandgroup to
* disable every button in the
* group when a particular button is pressed

* This line would appear in the Click event for
* the button that triggers the change.
This.Parent.SetAll("Enabled",.F.)

See Also

AMembers(), ParentClass, Scatter, This, ThisForm, ThisFormSet