Creating Arrays of Objects

See Also

You can declare and use arrays of an object type just as you declare and use an array of any data type. These arrays can be fixed-size or dynamic.

Arrays of Form Variables

You can declare an array of forms with Private, Dim, ReDim, Static, or Public in the same way you declare an array of any other type. If you declare the array with the New keyword, Visual Basic automatically creates a new instance of the form for each element in the array as you use the elements in the array.

Private Sub Command1_Click ()
   Dim intX As Integer
   Dim frmNew(1 To 5) As New Form1
   For intX = 1 To 5
      frmNew(intX).Show
      frmNew(intX).WindowState = vbMinimized
      ' To create minimized forms without having them
      ' first appear briefly at normal size, reverse
      ' the order of the two lines above.
   Next
End Sub

Pressing the command button to execute the code above will create five minimized instances of Form1.

Note   If you look at the Task Bar, you'll see Form1 six times. The extra instance of Form1 isn't minimized — it's the one you started with.

Arrays of Control Variables

You can declare an array of controls with Private, Dim, ReDim, Static, or Public in the same way you declare an array of any other type. Unlike form arrays, however, control arrays cannot be declared with the New keyword. For example, you can declare an array to be a specific control type:

ReDim ActiveImages(10) As Image

When you declare an array to be a particular control type, you can assign only controls of that type to the array. In the case of the preceding declaration, for example, you can only assign image controls to the array — but those image controls can come from different forms.

Contrast this with the built-in Controls collection, which can contain many different types of controls — all which must be on the same form.

Alternatively, you can declare an array of generic control variables. For example, you might want to keep track of every control that was dropped onto a particular control, and not allow any control to be dropped more than once. You can do this by maintaining a dynamic array of control variables that contains references to each control that has been dropped:

Private Sub List1_DragDrop(Source As VB.Control, _
      X As Single, Y As Single)
   Dim intX As Integer
   Static intSize As Integer
   Static ctlDropped() As Control
   For intX = 1 To intSize
      ' If the dropped control is in the array, it's
      ' already been dropped here once.
      If ctlDropped(intX) Is Source Then
         Beep
         Exit Sub
      End If
   Next
   ' Enlarge the array.
   intSize = intSize + 1
   ReDim Preserve ctlDropped(intSize)
   ' Save a reference to the control that was dropped.
   Set ctlDropped(intSize) = Source
   ' Add the name of the control to the list box.
   List1.AddItem Source.Name
End Sub

This example uses the Is operator to compare the variables in the control array with the control argument. The Is operator can be used to test the identity of Visual Basic object references: If you compare two different references to the same object, the Is operator returns True.

The example also uses the Set statement to assign the object reference in the Source argument to an element in the array.

For More Information   See "Is Operator" in the Language Reference.

Arrays are introduced in "Arrays" and "Dynamic Arrays" in "Programming Fundamentals."

For an easier way to keep track of objects, see "Creating Collections of Objects" later in this chapter.