Wiki Home

Add Property


Namespace: Wiki


Add Property is a method and a function, that allows you to add a new property to the VFP objects programmatically.
I've been exploring some code that is using Add Property in loops. The code doesn't Add Property during the Init of the class. In order to reset these new properties, Add Property is called again, specifying the new values. I believe it would be faster to set the property directly, calling Add Property once to create the property.

LOCAL lnElapsed, lnStart, loCmg
loForm = CREATEOBJECT("Form")
loForm.AddObject("cmg1","CommandGroup")
loCmg = loForm.cmg1
loCmg.AddObject("cmd1","CommandButton")
loCmd = loCmg.cmd1
loCmd.AddProperty("ilTesting",.F.)

ACTIVATE SCREEN

*First set the property directly.
lnStart = SECONDS()
FOR lnI = 1 TO 100000
  m.loCmd.ilTesting = .T.
ENDFOR lnI
lnElapsed = SECONDS() - m.lnStart
?m.lnElapsed


*Set the property via AddProperty.
lnStart = SECONDS()
FOR lnI = 1 TO 100000
  m.loCmd.AddProperty("ilTesting",.T.)
ENDFOR lnI
lnElapsed = SECONDS() - m.lnStart
?m.lnElapsed


If you can rely on the prop being there, then setting it is fine. But what if the prop doesn't exist? To be fair, your code should have
if !PemStatus(loCmd, "ilTesting", 5); m.loCmd.AddProperty("ilTesting",.T.) in the loop. Oddly enough, it still comes in at over 2x as fast. - ?cfk

Good point Carl! The original code IMO could have added the properties in the init, making it unnecessary to check for the existence of the property everywhere else. That's a lot of extra code to write everytime one want's to set a property... Since it's a loop, wouldn't the best thing be to prepare/test the property before starting the loop? -- Mike Yearwood

Mike, sure it a loop here, but what is the point of the loop (he says, rhetorically) - you wouldn't ever do something like this in real life.
Also, as a PERSONAL thing, I like to see things mean what they say and say what they mean, so Add Property() that is really just setting the value rubs me the wrong way. Same way as USE rubs me when it's really UNUSEing, for instance. -- Jim Nelson

Actually, Jim, the code did have a similar loop. It was only 42 iterations, but it made me wonder why the property was being added, when the property existed the last time the same property was added during a previous run of the same loop. So it did come from real life.

In VFP8 there's also the Add Property Function that allows to add property to the objects that don't have Add Property method. -- Sergey Berezniker

Contributor(s): Mike Yearwood
Category VFP Methods Category Code Samples Category Performance Category VFP Tips And Tricks Category Needs Refactoring
( Topic last updated: 2006.02.15 11:22:41 PM )