Wiki Home

Create Object


Namespace: WIN_COM_API
Culled from VFP help: CREATEOBJECT() creates an object from a class definition or an Automation-enabled application.

Syntax: CREATEOBJECT(classname [, eParameter1, eParameter2, ...])

Visual FoxPro searches for the class along the Visual FoxPro Invocation Stack.
From the Hackers Guide: "Because you can omit the class information if you want, and then New Object() behaves just like Create Object(), we suspect we'll be using New Object() all the time..."

Consult the Data Session page, item g for advice about employing PUBLIC memvars to assign CREATEOBJECT() in forms with a private datasession. -- Steven Blake

NOTE: I'm not sure where but I think I heard or read that tests were done on instantiation times and Create Object is slightly faster for multiple calls where CLASSLIB is not changed whereas New Object is faster if the classlibrary is changed frequently. Correct me if I'm wrong. - Todd Chandler

See also: Visual FoxPro Invocation Stack

Note that classname cannot be the Visual FoxPro OLEContainer control base class.
Cousin to the way cool and totally powerful Create Object Ex function.


Hey.

I've got a question about returning hidden objects through public methods.

I have a custom based record object and I add a hidden table object member variable to that.

In the init function, I am calling createobject for a custom based table object and assigning it to that hidden table variable.

I also have a get_table method that returns the table object contained in the hidden varaible.

define class Crecord as custom
hidden m_oTable

procedure Init
this.m_oTable = createobject(...)
endproc

function Get_Table as object
return this.m_oTable
endfunc
enddefine

Assume that the table object gets initialized inside the record object's Init and the table variable gets a valid table object.

Okay. Now I want to call a method from the table object of the record object.

oRec = createobject("Crecord", ...)

Assuming that DoTableMethod() is a public table method, why does the following code give me an error?

oRec.Get_Table().DoTableMethod()

"Invalid subscript reference"


I tried doing this:

local oTable as object
oTable = oRec.Get_Table()
oTable.DoTableMethod()

and it worked, but this is a bad solution to my problem. I don't want an extra local pointing to my hidden objects and I don't want the added extra line of code.

Any ideas?

BTW: I already tried using :: instead of (). after Get_Table to no avail.


Thanks!

Jason Timms

Jason, you are stuck with the second option as the method is not an object reference. You have to store the returned object reference before you can call its methods. --JimBooth
Thanks Jim. Doing that and sticking to my guns with my hidden members beats exposing them just to save a few lines of extra code. I can always release the local once I'm done.

Jason Timms

Category VFP Functions
( Topic last updated: 2007.01.09 10:25:37 AM )