Page 1 of 1

Array2ArrayObject(aData, aFld)

Posted: Thu Feb 23, 2017 6:22 am
by unixkd
Hi all,

I am trying to create a function that will receive a 2-dimensional array e.g. aData := Directory() and create Return an array of objects, where each element of the array can be accessed via the iVars supplied in aFld.

When I run the program I get error: Access to instance variable not allowed within instance-object

Thanks.

Joe

#include "CLASS.CH"
*
Function Array2ArrayObject(aData, aFld)
Local aRec[0], oaArray[0], nAttr
Local oRec := ClassObject( "Array2ArrayObject" )
IF oRec <> NIL
Return(oRec)
EndIf
IF Empty(aData)
Return nil
EndIf
DEFAULT aFld := aData[1]
nAttr := CLASS_EXPORTED + VAR_INSTANCE
aEval(aFld, {|e| e := {e, nAttr }},nil,nil,.t.)
oRec := ClassCreate( "Array2ArrayObject", nil, aFld)

aEval(aData, {|e|aRec := e, aEval(aRec, {|e,i| oRec:&(aFld[i,1]) := e}), Aadd(oaArray,oRec) },2)
ClassDestroy(oRec)
Return(oaArray)

Re: Array2ArrayObject(aData, aFld)

Posted: Thu Feb 23, 2017 6:41 am
by rdonnay
I recommend that you use the new DataObject() class for this. This is new in Xbase++ 2.0 and is a much better implementation of dynamic class creation for this kind of application.

Code: Select all

#INCLUDE "dcdialog.CH"

FUNCTION Main()

LOCAL aDir := Directory(), aObjects

aObjects := Array2Object(aDir)

wtf aObjects pause

RETURN nil

* -----------

FUNCTION Array2Object(aArray)

LOCAL aObjects[Len(aArray)], i, j, aSubArray

FOR i := 1 TO Len(aObjects)
  aObjects[i] := DataObject():new()
  aSubArray := aArray[i]
  FOR j := 1 TO Len(aSubArray)
    aObjects[i]:&('Element'+DC_PadZero(Str(j,2))) := aSubArray[j]
  NEXT
NEXT

RETURN aObjects

Re: Array2ArrayObject(aData, aFld)

Posted: Thu Feb 23, 2017 7:01 am
by unixkd
Thanks Roger

I still use 1.9SL1 and need to accomplish this.

Joe

Re: Array2ArrayObject(aData, aFld)

Posted: Thu Feb 23, 2017 7:24 am
by rdonnay
Give me a complete program that I can compile and run.

What are you passing to Array2ArrayObject() ?

Re: Array2ArrayObject(aData, aFld)

Posted: Thu Feb 23, 2017 7:52 am
by unixkd
Function MyArray2ObjectTest()
Local aFld := {"F_NAME","F_SIZE","F_WRITE_DATE","F_DATE","F_WRITE_TIME","F_TIME","F_ATTR","F_EA_SIZE","F_CREATION_DATE",;
"F_CREATION_TIME","F_ACCESS_DATE","F_ACCESS_TIME"}

Local aDir := Directory()

oDir := Array2ArrayObject(aDir, aFld)

wtf oDir

Return nil

Re: Array2ArrayObject(aData, aFld)

Posted: Thu Feb 23, 2017 9:01 am
by rdonnay
You didn't create a :new() instance of the class.
That's what caused the error.

Secondly, you cannot destroy until you are done using the objects.

Code: Select all

#include "CLASS.CH"
#INCLUDE "dcdialog.CH"

FUNCTION Main()

Local aFld := {"F_NAME","F_SIZE","F_WRITE_DATE","F_DATE","F_WRITE_TIME","F_TIME","F_ATTR","F_EA_SIZE","F_CREATION_DATE",;
"F_CREATION_TIME","F_ACCESS_DATE","F_ACCESS_TIME"}

Local aDir := Directory(), oDir, oRec

oDir := Array2ArrayObject('Directory', aDir, aFld, @oRec )

wtf oDir pause

ClassDestroy(oRec)   <<<<<<<<<<<<<<<

Return nil

* ----------

Function Array2ArrayObject(cClassName, aData, aFld, oRec )

Local aRec[0], oaArray[0], nAttr
Local oRec := ClassObject( cClassName )

IF oRec <> NIL
  Return(oRec)
EndIf

IF Empty(aData)
  RETURN nil
EndIf

DEFAULT aFld := aData[1]

nAttr := CLASS_EXPORTED + VAR_INSTANCE

aEval(aFld, {|e| e := {e, nAttr }},nil,nil,.t.)

oRec := ClassCreate( cClassName, nil, aFld):new()  <<<<<<<<<<<<<

aEval(aData, {|e|aRec := e, aEval(aRec, {|e,i| oRec:&(aFld[i,1]) := e}), Aadd(oaArray,oRec) },2)

Return(oaArray)