Passing Array Between Forms

Hi

I want to pass an array of arbitrary size from one form to another.  The array would contain employee numbers PIC 9(9) and a flag to say whether the employee had been selected.

Basically, from the main form the user can press a List... button which calls up a subform.  This subform contains a multiselect ListView which is populated from a flat file of employees.  There could be 1 employee in the file, or thousands, so no limit.  When the user has selected the employees they want and the subform is closed, then the selected employees are passed back to the main form for processing.

Currently, I am using the linkage in public methods to pass data between forms, but the size of the data is known.  I know SteveC had a similar problem recently but the complete solution was not provided.

I need to know the best way to go about passing the information required back to the main form.  Thank you.

  • Hi Brendan, I'm not certain I fully understand what you're trying to do, but could you do something like:

    class-id Employee.

    01 Number binary-long.

    01 Selected condition-value.

    ...

    end class.

    You can then build an array or a List of instances of this class:

    01 employees type Employee occuurs any.

    ...or:

    01 employees list[type Employee].

    ...and the list or array can then be passed between methods as a parameter.

    Robert.

  • Verified Answer

    The following is an example which demonstrates the following:

    1. main form populates an array with an Employee object like Robert explained.
    2. Pass this array to a subform where it will be stored in working-storage
    3. Subform places the emp-nos from array into a listview control and returns
    4. main form displays subform
    5. User selects one or more emp-nos from listview and clicks button
    6. Subform closes and returns to main form
    7. main form calls method iun subform to get array of selected items
    8. main form then displays selected items in a listbox

    main form:

     

      class-id passarray.Form1 is partial
                     inherits type System.Windows.Forms.Form.
           
           working-storage section.
           01 empArray type Employees occurs any.
           method-id NEW.
           procedure division.
               invoke self::InitializeComponent
               goback.
           end method.
    
           method-id button1_Click final private.
           procedure division using by value sender as object e as type System.EventArgs.
           
               set size of empArray to 5
               perform varying sub1 as binary-long from 1 by 1 until sub1 > 5
                  set empArray(sub1) to new Employees
                  set empArray(sub1)::emp-no to sub1
                  set empArray(sub1)::checked to false
               end-perform
               
               declare mysubform as type passarray.subform = new passarray.subform
               invoke mysubform::setArray(empArray)
               invoke mysubform::ShowDialog
               
               set empArray to mysubform::getArray
               
               invoke listBox1::Items::Clear
               perform varying ret-emps as type Employees thru empArray
                  if ret-emps::checked
                     invoke listBox1::Items::Add(ret-emps::emp-no)
                  end-if
               end-perform.
               
           end method.
          
           end class.
           class-id Employees.
           working-storage section.
           01 emp-no    pic 9(9) property.
           01 checked   condition-value property.
           procedure division.
           
           end class.
    



    subform:

           class-id passarray.subform is partial
                     inherits type System.Windows.Forms.Form.
    
           working-storage section.
           01 empArray type Employees occurs any.
           method-id NEW.
           procedure division.
               invoke self::InitializeComponent()
               goback.
           end method.
    
           method-id button1_Click final private.
           procedure division using by value sender as object e as type System.EventArgs.
               invoke self::Close
           end method.
    
           method-id setArray public.
           procedure division using by value myArray as type Employees occurs any.
               
               set empArray to myArray
               
               perform varying myemployee as type Employees thru myArray
                  declare lvi as type ListViewItem = new ListViewItem(myEmployee::emp-no::ToString)
                  invoke listView1::Items::Add(lvi)
               end-perform
                     
               goback.
           end method.
           method-id getArray public.
           procedure division returning myArray as type Employees occurs any.
               
               perform varying mylistitem as type ListViewItem thru listView1::SelectedItems
                  perform varying myemployee as type Employees thru empArray
                     if myemployee::emp-no = type Int32::Parse(mylistitem::Text) 
                        set myemployee::checked to true
                        exit perform
                     end-if
                  end-perform
               end-perform
               set myArray to empArray
               goback.
           end method.
           
           end class.