Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Createobject OR addobject OR NewObject
Message
From
21/07/2002 18:31:04
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00680926
Message ID:
00680942
Views:
23
>I'm aware of three functions for adding an object to FoxPro. CreateObject and NewObject get variable assignments while AddObject adds the class to a "collection" on the parent object.
>
>But what are the differences?

NewObject is like CreateObject and Addobject except that you don't have to use SET CLASSLIB, and it can be slower--much slower according to some reports. I use NewObject mostly because relying on set classlib breaks down encapsulation, IMO. It makes an object's behavior rely on or change the environment. You can write good citizen code of course, but that makes for awkward code and the more code you write, the more chances of bugs you introduce. That said, if I have to create objects in a loop where performance matters, I'd use CreateObject.

NewObject is a function as well as method.

AddObject, NewObject, and CreateObject all create an object reference. AddObject also, as you say, adds the object to parent's object collection as does NewObject--the method. You won't be able to add all kinds of objects this way. If you want a textbox to have a reference to a form, for example, you have create a property then assign the object to it with one of the functions. If you want to use this.parent you have to use one of the methods.

>Is there a time when each one is more appropriate?

When an object depends on the parent--is created by it and dies with it, then, if you can, use the method.

>Is one better "in general" than the others?

Not really. They're all three useful. And I keep hoping that the performance of NewObject will improve. :)
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform