Wiki Home

New Object


Namespace: VFP
New in VFP6, NewObject(cClassName [, cModule [, cInApplication [, eParameter1, eParameter2, ...]]]) creates a new class or object directly from a .vcx visual class library or program.

Note: When you specify a class library or application, NEWOBJECT() always loads this library, even if it has already been loaded with SET CLASSLIB TO. This is especially important, if you use this function in forms or classes that are not compiled into the EXE, but the class that you want to create is. In this case, VFP might not be able to find the class library and NEWOBJECT() fails. Also, because of this behaviour, NEWOBJECT() might actually be slower than CREATEOBJECT().

See also Create Object
This behavior can be a PITA, however, especially in a development environment. For example, suppose you provide a class to someone else and that class instantiates other classes in the same VCX. Say they put the VCX some place that's not the current directory or in the VFP path, but they do use SET CLASSLIB to open it, and then instantiate your class. Your problem:In this case you can use NEWOBJECT("Class",This.ClassLib). But in general, I'd prefer the MAKEOBJECT() approach, as it works in a broader range of application setups.
For this reason, I use a PRG called MAKEOBJECT that has the best of both worlds:Here's the code for MAKEOBJECT (excuse the formatting; the @ sign is messing up indents):
lparameters tcClass, ;
    tcLibrary, ;
    tcInApp, ;
    tuParm1, ;
    tuParm2, ;
    tuParm3, ;
    tuParm4, ;
    tuParm5, ;
    tuParm6
local lcLibrary, ;
    llLibrary, ;
    lnParms, ;
    loObject
lcLibrary = iif(empty(tcLibrary) or upper(tcLibrary) $ set('CLASSLIB') or ;
    upper(tcLibrary) $ set('PROCEDURE'), '', tcLibrary)
llLibrary = empty(lcLibrary) or file(tcLibrary) or ;
    file(tcLibrary + '.VCX') or file(tcLibrary + '.PRG') or ;
    file(tcLibrary + '.FXP')
lnParms   = pcount()
do case
    case lnParms = 1
        loObject = createobject(tcClass)
    case not llLibrary
        loObject = .NULL.
    case lnParms = 2
        loObject  = newobject(tcClass, lcLibrary)
    case lnParms = 3
        loObject  = newobject(tcClass, lcLibrary, tcInApp)
    case lnParms = 4
        loObject  = newobject(tcClass, lcLibrary, tcInApp, @tuParm1)
    case lnParms = 5
        loObject  = newobject(tcClass, lcLibrary, tcInApp, @tuParm1, ;
            @tuParm2)
    case lnParms = 6
        loObject  = newobject(tcClass, lcLibrary, tcInApp, @tuParm1, ;
            @tuParm2, @tuParm3)
    case lnParms = 7
        loObject  = newobject(tcClass, lcLibrary, tcInApp, @tuParm1, ;
            @tuParm2, @tuParm3, @tuParm4)
    case lnParms = 8
        loObject  = newobject(tcClass, lcLibrary, tcInApp, @tuParm1, ;
            @tuParm2, @tuParm3, @tuParm4, @tuParm5)
    case lnParms = 9
        loObject  = newobject(tcClass, lcLibrary, tcInApp, @tuParm1, ;
            @tuParm2, @tuParm3, @tuParm4, @tuParm5, @tuParm6)
endcase
return loObject
-- Doug Hennig
NEWOBJECT.PRG mimics the NEWOBJECT() function in VFP 5, so you can write code that works in both versions (excuse the formatting; the @ sign is messing up indents):
lparameters tcClass, ;
    tcLibrary, ;
    tcInApp, ;
    tuParm1, ;
    tuParm2, ;
    tuParm3, ;
    tuParm4, ;
    tuParm5, ;
    tuParm6
local lcLibrary, ;
    lcInApp, ;
    loObject

* If the class library is specified, SET CLASSLIB to it if necessary.

do case
    case type('tcLibrary') <> 'C' or empty(tcLibrary) or ;
        upper(tcLibrary) $ upper(set('CLASSLIB'))
        lcLibrary = ''
    case file(tcLibrary)
        lcLibrary = upper(tcLibrary)
    otherwise
        lcLibrary = upper(locfile(tcLibrary, ;
            'Visual Class Library (*.vcx):VCX;Program (*.prg):PRG', tcLibrary))
endcase
if not empty(lcLibrary)
    lcInApp = iif(type('tcInApp') = 'C' and not empty(tcInApp), ;
        'in ' + tcInApp, '')
    set classlib to (lcLibrary) &lcInApp additive
endif not empty(lcLibrary)

* Create the object.

do case
    case pcount() < 4
        loObject = createobject(tcClass)
    case pcount() = 4
        loObject = createobject(tcClass, @tuParm1)
    case pcount() = 5
        loObject = createobject(tcClass, @tuParm1, @tuParm2)
    case pcount() = 6
        loObject = createobject(tcClass, @tuParm1, @tuParm2, @tuParm3)
    case pcount() = 7
        loObject = createobject(tcClass, @tuParm1, @tuParm2, @tuParm3, ;
            @tuParm4)
    case pcount() = 8
        loObject = createobject(tcClass, @tuParm1, @tuParm2, @tuParm3, ;
            @tuParm4, @tuParm5)
    case pcount() = 9
        loObject = createobject(tcClass, @tuParm1, @tuParm2, @tuParm3, ;
            @tuParm4, @tuParm5, @tuParm6)
endcase

* Release the library if we opened it (and it's still open) and return a
* reference to the object we created.

if not empty(lcLibrary) and lcLibrary $ upper(set('CLASSLIB'))
    release classlib (lcLibrary)
endif not empty(lcLibrary) ...
return loObject
-- Doug Hennig
Contributors: ? Christof Wollenhaupt Doug Hennig
Category VFP Functions
( Topic last updated: 1999.11.01 12:40:58 PM )