How Can I Create an Array of Forms?

cementboot

Newcomer
Joined
Aug 27, 2005
Messages
8
How can I refer to an form in array of forms?

Hi,

How can I refer to an form in array of forms?

In my project, I have created an array of forms in a module like this:

Code:
Module FormsArray

Public ArrayOfForms(25) As clsDummyForm


Public Sub CreateFormsArray(ByVal i As Integer)
    
   ArrayOfForms(i) = New clsDummyForm

End Sub


Public Sub CloseForms(ByVal i As Integer)
    
    ArrayOfForms(i).Close()

End Sub

End Module
(clsDummyForm in the code above is a class in which I am inheriting System.Windows.Forms.Form)

The problem occurs when I try to use the CloseForms() sub from another form like this:

Code:
Public Class Form1

Private Sub CloseAllForms()
    
   For x As Integer = 1 To 25
         CloseForms(x)
    Next

End Sub

End Class

I get the error "Object is not set to an instance of an object".

Any suggestions on how I can fix this? -thanks
 
Last edited:
First of all I would not suggest using Modules but that is a topic for another discussion ;).

Are you sure you called the initialization sub for all the forms in your array? This error is most likely occuring due to not initialzing some form objects in the array and trying to use them.
 
Mutant, thanks for your help! I think I figured it out now. I wasn't initializing all of the forms and therefore some of them had the value of nothing.
 
Back
Top