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.

AddProperty

This method lets you add properties to objects both at design-time and at runtime.

Usage

oObject.AddProperty( cNewProp [, eInitialValue ] )

Though it’s not good OOP, sometimes you really just need one more property in an object after you’ve instantiated it. Maybe it deals with a rare situation. Maybe you’re working with someone else’s class library (with no source available) and don’t want to add another level of subclassing.

Paul Bienick wrote an AddProp.FLL in October of 1995 to add properties to existing objects, but there was no native language support for this technique until VFP 6. This method lets you break the rules and add a property on the fly without the need for an outside library.

AddProperty works at design-time, too, where we think it’s a far better idea. Like several other additions in this version, it makes builders even easier to build. In combination with Access and Assign methods, this can make adding properties “on demand” a reality.

The best reason we’ve heard to use AddProperty at runtime is to create classes to represent records. You can add a property for each field, then handle tables just like other objects. Another really good reason is to create a parameter object to pass multiple parameters. An object like this is especially useful when you need more than one return value from a method. Create an object, use AddProperty to create a property for each item that needs to be passed, and then pass just the object reference into the method. The method can modify the properties for whatever it needs to return.

Example

* Assume you have an abstract Record class.
* At runtime, add properties to represent the fields.
* Assumes table is open in current work area.
oData = CreateObject("Record")
FOR nFld = 1 TO FCOUNT()
   oData.AddProperty(FIELD(nFld))
ENDFOR

See Also

Access, Assign, ReadExpression, ReadMethod, WriteExpression, WriteMethod