Compartir a través de


Object and Member Arrays

You can create arrays containing objects and as class members.

Object Arrays

You can create arrays that contain objects. When creating arrays that contain objects, keep in mind the following considerations:

  • You cannot assign an individual object to all the elements in an array using a single command. You must assign the object to each element of the array individually.

  • You cannot assign a value to the property of an entire array.

    For example, the following command results 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.

For example, the following code creates an array named MyArray that holds five command buttons using a loop structure:

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

Class Member Arrays

You can include arrays as class members and store objects in them.

The following example creates a form that contains an object created from the custom Container class, ButtonList. The ButtonList class contains a member that is an array named, aChoices, in which each element contain a different control object.

When you run the example from a program (.prg) file, a form appears with two command buttons and a check box. When the user clicks a command button or selects the check box on the form, Visual FoxPro passes the index number of the array element that contains the control to the Click event through the tnIndex parameter. A DO CASE command structure displays a window with the number of the element.

different code depending on which button was clicked.

loForm = CREATEOBJECT("Form")
loForm.AddObject("cntButtons", "ButtonList")
loForm.cntButtons.SetAll("Visible", .T.)
loForm.cntButtons.Visible = .T.
loForm.Show(1)

DEFINE CLASS ButtonList AS Container
   Height = 100
   Width = 130
   BorderWidth = 0
   
   DIMENSION aChoices[3]
   
   ADD OBJECT aChoices[1] AS CommandButton WITH ;
      Top = 10, ;
      Height = 20, ;
      Left = 10, ;
      Caption = "Element One"
   ADD OBJECT aChoices[2] AS CommandButton WITH ; 
      Top = 40, ;
      Left = 10, ;
      Height = 20, ;
      Caption = "Element Two" 
   ADD OBJECT aChoices[3] AS CheckBox WITH ;
      Top = 70, ;
      Left = 10, ;
      Caption = "Element Three"
      
   PROCEDURE aChoices.Click
      LPARAMETER tnIndex
      DO CASE
         CASE tnIndex = 1
            WAIT WINDOW "1"
         CASE tnIndex = 2
            WAIT WINDOW "2"
         CASE tnIndex = 3
            WAIT WINDOW "3"
      ENDCASE
   ENDPROC
ENDDEFINE

See Also

Concepts

Objects in Visual FoxPro

Reference

Arrays

Adding Objects to a Container Class

Other Resources

Working with Objects in Visual FoxPro