Partager via


Arrays of Members and Objects

You can define members of classes as arrays. In the following example, choices is an array of controls:

DEFINE CLASS MoverListBox AS CONTAINER
DIMENSION choices[3]
ADD OBJECT lstFromListBox AS LISTBOX
ADD OBJECT lstToListBox AS LISTBOX
ADD OBJECT choices[1] AS COMMANDBUTTON
ADD OBJECT choices[2] AS COMMANDBUTTON
ADD OBJECT choices[3] AS CHECKBOX
PROCEDURE choices.CLICK
   PARAMETER nIndex
   DO CASE
      CASE nIndex = 1
         * code
      CASE nIndex = 2
         * code
      CASE nIndex = 3
         * code
   ENDCASE
ENDPROC
ENDDEFINE

When the user clicks a control in an array of controls, Visual FoxPro passes the index number of the control to the Click event procedure. In this procedure, you can use a CASE statement to execute different code depending on which button was clicked.

Creating Arrays of Objects

You can also create arrays of objects. For example, MyArray holds five command buttons:

DIMENSION MyArray[5]
FOR x = 1 TO 5
   MyArray[x] = CREATEOBJECT("COMMANDBUTTON")
ENDFOR

There are some considerations to keep in mind with arrays of objects:

  • You can't assign an object to an entire array with one command. You need to assign the object to each member of the array individually.

  • You can't assign a value to a property of an entire array. The following command would result in an error:

    MyArray.Enabled = .F.
    
  • When you redimension an object array so that it is larger than the original array, the new elements are initialized to false (.F.), as is the case with all arrays in Visual FoxPro. When you redimension an object array so that it is smaller than the original array, the objects with a subscript greater than the largest new subscript are released.

See Also

Writing Class Definitions Programmatically | Object Reference Creation | Data Storage with Objects | Object and Data Integration | Object-Oriented Programming