只需在类级别声明方法frm2
之外的数组即可。Sub
为了将来参考,在类级别声明的变量称为字段。字段的范围仅限于类,因此类中的任何方法都可以访问它们。例如:
Public Class MyClass
Private frm2() As Form
Public Sub CreateForms()
Dim a As Integer = Screen.AllScreens.Length
ReDim frm2(a)
For b As Integer = 0 To a - 1
frm2(b) = New Form()
' ... set attributes to the form here
frm2(b).Show()
Next
End Sub
End Class
对于它的价值,我建议使用 aList(Of Form)
而不是数组。这样您就不必担心重新调整它的大小,例如:
Public Class MyClass
Private forms As New List(Of Form)()
Public Sub CreateForms()
For i As Integer = 0 To Screen.AllScreens.Length - 1
Dim frm2 As New Form()
' ... set attributes to the form here
frm2.Show()
forms.Add(frm2)
Next
End Sub
End Class