Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return an Array from a Form 2

Status
Not open for further replies.

cmergal

MIS
Oct 16, 2002
18
US
Hello:

I have a form that I want to return an array from it. I call the form with a DO FORM frmName TO laretval. The form manipulates the data and stores the values in an array property and returns the property. The problem is that laretval is not an array and only stores the first element of the passed array. I tried passing it by reference without any luck. Finally I tried pre defining laRetval as an array and no luck. This cannot be that difficult what am I missing?

TIA,
 
The array needs to be declared as PUBLIC or associated with an object that stops it from going out of scope when the form in closed.

If you need more info, there have been several threads in forum184 over the last couple months on various ways to do this... I'd keyword search on PUBLIC.

Brian
 
Cmergal,

The way I have always done this is to make the array a propety of the calling form, and to reference it directly in the called form.

Given that the calling form has an array property named aArray:

- In the child form, create a property called (say) oCaller.

- In the Load event of the child form:
THISFORM.oCaller = _SCREEN.ActiveForm

- At any other point in the child form, you can store values into the array by referencing THISFORM.oCaller.aArray.

- No need to return a value. When you get back to the caller, the array will contain the values you stored there.

I don't know if this is the best way to do this, but it has always worked well for me. It has the advantage that you can use THISFORM.oCaller to access any of the other properties of the calling form as well.

Hope this helps.

Mike


Mike Lewis
Edinburgh, Scotland

My Visual Foxpro web site: My Crystal Reports web site:
 
Here's an alternative:

Return an object from the form, with one property which is an array:

in the form's Unload event:
Code:
oRet = CREATEOBJECT('Empty') && or 'CUSTOM' if pre VFP8
AddProperty(oRet,'arr[5]',.f.)
** or, pre VFP8:
**   oRet.AddProperty('arr[5]',.f.)
DIMENSION oRet.arr[alen(THISFORM.aMyarray,1),alen(THISFORM.aMyarray,2)]
ACOPY( THIFORM.aMyArray, oRet.arr )
** OR
oRet.Arr[1] = 'value 1'
*..etc.
RETURN oRet
Then, in the calling code:
Code:
DO FORM frmName TO loRetVal
DIMENSION laRetVal[alen(loRetVal.arr,1),alen(loRetVal.arr,2)]
ACOPY(loRetVal.arr,laRetVal)
* or just use loRetVal.arr as your array
 
Hello cMergal.

What I did way back in VFP 3.0 was to create a parameter class with a single array property like so:

Code:
**************************************************
*-- Class:        xparameters 
*-- ParentClass:  cusbase 
*-- BaseClass:    custom
*-- Time Stamp:   03/22/04 05:29:01 PM
*-- Parameter object - contains an array property - used to return an array by using code like this: RETURN CREATEOBJECT( 'xParameters', @laArray )
*
DEFINE CLASS xparameters AS cusbase

  Name = "xparameters"
  DIMENSION aparameters[1]


PROCEDURE Init
  LPARAMETERS taParameters

  *** Copy array passed by reference, before it goes out of scope
  ACOPY( taParameters, THIS.aParameters )
ENDPROC

ENDDEFINE
*
*-- EndDefine: xparameters
**************************************************

Then, to return an array from a form method or a function, all I had to do was this:

Code:
RETURN CREATEOBJECT( [xparameters], @laMyArray )

Marcia G. Akins
 
Here's another way of calling a form and having it update an array.

Darrell

Code:
* Define a private or public name and dimension an array
* with that name. (private is obviously safer)
PRIVATE aArray
DIME aArray[1]

LOCAL oForm

* Pass the name of the private array
oForm = CREATEOBJECT("clsForm","aArray")
oForm.SHOW()
READ EVENTS

* View the array in the debugger to verify the array was updated
* You may need to close and reopen the locals window in the debugger
SET STEP ON

DEFINE CLASS clsForm AS FORM
  DOCREATE = .T.
  AUTOCENTER = .T.
  CAPTION = "Passing an array to be updated to a form"

  cArrayName = ''

  ADD OBJECT cmdUpdateArray AS COMMANDBUTTON WITH;
    CAPTION = "Update Array", ;
    WIDTH = 100, ;
    HEIGHT = 30, ;
    TOP = THIS.HEIGHT / 2 - 15, ;
    LEFT = THIS.WIDTH / 2 - 50

  PROCEDURE INIT
    LPARAM aArrayIn
    * Save the name of the external private array
    THIS.cArrayName = aArrayIn
  ENDPROC

  PROCEDURE UpdateArray()
    * Update the external private array

    LOCAL a, i

    a = "dime "+THIS.cArrayName+"[100]"
    &a

    FOR i = 1 TO EVAL("alen("+THIS.cArrayName+")")
      a = THIS.cArrayName+"[i]="+TRANSFORM(i)
      &a
    NEXT

  ENDPROC

  PROCEDURE DESTROY
    CLEAR EVENTS
  ENDPROC

  PROCEDURE cmdUpdateArray.CLICK()
    THISFORM.UpdateArray()
    THISFORM.RELEASE()
  ENDPROC

ENDDEFINE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top