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.

CREATEOBJECT(), NewObject()

These functions instantiate objects based on a specified class. The class may be one of the FoxPro base classes, a user-defined class, or a class from a registered OLE server.

Usage

oObject = CREATEOBJECT( cClassName
                        [, uParam1 [, uParam2 [, ...] ] ] )
oObject = NEWOBJECT( cClassName [, cClassLib [, cLibInApp
                     [, uParam1 [, uParam2 [, ... ] ] ] ] ] )

Parameter

Value

Meaning

cClassName

 

FoxPro class name

Create an object of the specified FoxPro class.

COM Object class name

Start the COM Object, if it's not running, and either create or get a handle to an object of the specified class.

uParam1, uParam2, ...

Any expressions

Parameters to pass to the Init method of the class.

cClassLib

Character

The path and filename of the library, program or application containing cClassName. An extension of VCX is assumed, if you omit it.

cLibInApp

Character

The path and filename (including extension) of the application containing cClassLib.

oObject

Object

A reference to the newly created object.

NewObject() was added in VFP 6 and lets you instantiate things without having to issue SET CLASSLIB or SET PROCEDURE first. Instead, you can incorporate the information about where to find the relevant class into the command. Because you can omit the class information if you want, and then NewObject() behaves just like CREATEOBJECT(), we suspect we’ll be using NewObject() all the time, if we can only retrain our fingers from typing o=CREA(“whatever”).

NewObject() even lets you deal with classes whose definition exists only in an application (APP or EXE or whatever). You don’t have to have the actual class library available. Just point to the class library and the application:

o = NewObject("MyClass", "MyClassLib", "MyApp.APP")

and VFP is able to dig through the app file and find the class definition.

We think this is really cool stuff. It’ll make it much easier for people to distribute class libraries without having to distribute the source.

For automation objects, cClassName consists of the application name followed by the class name. For example, to open Word 97, you use:

oWord = CreateObject("Word.Application")

See “It Was Automation, You Know” for more on Automation.

Almost every object has an Init method that fires when the object is created. (The few that don’t are those created in oddball ways, such as SCATTER NAME.) You can pass parameters to the Init method of FoxPro objects by including them in CREATEOBJECT() or NewObject(). Beware, though: When container objects are instantiated, the Init methods of the contained objects execute before the Init of the container. For example, the Inits from all the controls in a form fire before the Init for the form.

Passing parameters to Init is a good way to set properties of an object at creation time. In the Init method, assign the values of the passed parameters to the appropriate properties.

Example

oMine1 = CREATEOBJECT("MyClass", 37, "Mustang")
? oMine1.Flivver  && Returns 37
? oMine1.Giblet   && Returns "Mustang"

oMine2 = CREATEOBJECT("MyClass")
? oMine2.Flivver  && Returns 0
? oMine2.Giblet   && Returns ""

oMine3 = CREATEOBJECT("MyClass", 14)
? oMine3.Flivver  && Returns 14
? oMine3.Giblet   && Returns ""

DEFINE CLASS MyClass AS CUSTOM

   * add some properties
   Flivver = 0
   Giblet = ""

PROCEDURE Init

   LPARAMETERS nFlivver, cGiblet

   IF NOT EMPTY(nFlivver)
      This.Flivver = nFlivver
   ENDIF
   IF NOT EMPTY(cGiblet)
      This.Giblet = cGiblet
   ENDIF
ENDPROC

ENDDEFINE

* If the definition above is stored in MyClass.PRG
* you could also:
oMine4 = NewObject("MyClass","MyClass.PRG")

See Also

AddObject, Define Class, Do Form, GetObject(), Init, NewObject