CREATEOBJECT( ) Function
Creates an object from a class definition or an Automation-enabled application.
CREATEOBJECT(ClassName [, eParameter1, eParameter2, ...])
Return Values
Object
Parameters
ClassName
Specifies the class or OLE object from which the new object is created. Visual FoxPro searches for the class or OLE object in the following order:- Visual FoxPro base classes.
- Classes in the current program.
- Classes in .vcx class libraries opened with SET CLASSLIB.
- Classes in procedure files opened with SET PROCEDURE.
- Classes in the Visual FoxPro program execution chain.
- The OLE registry if SET OLEOBJECT is ON.
OLE objects are created using the following syntax for ClassName:
ApplicationName.Class
For example, to create a Microsoft Excel worksheet (which supports Automation), you can use the following syntax:
x = CREATEOBJECT('Excel.Sheet')
When this code is run, Microsoft Excel is started (if not already running), and a new worksheet is created.
A class library can have an alias. To specify an object in a class library with an alias, include the class library alias followed by a period and the object name.
Note that ClassName cannot be the Visual FoxPro OLE Container control base class.
eParameter1, eParameter2, ...
These optional parameters are used to pass values to the Init event procedure for the class. The Init event is executed when you issue CREATEOBJECT( ) and allows you to initialize the object.
Remarks
Use CREATEOBJECT( ) to create an object from a class definition or an application that supports Automation, and assign a reference to the object to a system variable or array element.
Before you can create an object from a user-defined class, the user-defined class must first be created with DEFINE CLASS, or it must be available in a .vcx visual class library opened with SET CLASSLIB.
Use = or STORE to assign a reference to the object to a system variable or array element. If an object assigned to a system variable or array element is released, the system variable or array element contains the null value. Use RELEASE to remove the system variable or array element from memory.
Example
The following example uses DEFINE CLASS and CREATEOBJECT( ) to create two custom classes named FormChild and FormGrandChild from the Visual FoxPro Form base class. ACLASS( ) is used to create an array named gaNewarray
containing the class names, which are then displayed.
CLEAR
* Verify current class library setting
cCurClassLib=SET("CLASSLIB")
IF LEN(ALLTRIM(cCurClassLib))=0
cCurClassLib="None"
ENDIF
WAIT WINDOW "Current class library is: " + cCurClassLib + CHR(13);
+ "Press any key to continue..."
frmMyForm = CREATEOBJECT("FormGrandChild")
* Create an array
FOR nCount = 1 TO ACLASS(gaNewarray, frmMyForm)
? gaNewarray(nCount) && Display the names of the classes
ENDFOR
RELEASE frmMyForm
* Create FormChild from FORM baseclass
DEFINE CLASS FormChild AS FORM
ENDDEFINE
* Create FormGrandChild from user-defined FormChild class
DEFINE CLASS FormGrandChild AS FormChild
ENDDEFINE
See Also
_BROWSER | COMCLASSINFO( ) | CREATEOBJECTEX( ) | DEFINE CLASS | GETOBJECT( ) | RELEASE | SET CLASSLIB | SET OLEOBJECT