构造函数和垃圾回收 高分重谢
Sub ChildFrm(ByVal n As Integer, ByVal str_name As String)
Dim frm() As Form = Me.MdiChildren
Dim f() As Form = {New Form2, New Form3, New Form4}
If frm.Length = 0 Then '子窗体为空
Dim objfrm = f(n)
objfrm.MdiParent = Me
objfrm.Show()
Exit Sub
End If
Dim Fm As Form
Dim B As Boolean = False
For Each Fm In frm
If Fm.Name = str_name Then
Fm.Activate()
Exit Sub
Else
B = True
End If
Next
If B = True Then
Dim objfrm = f(n)
objfrm.MdiParent = Me
objfrm.Show()
End If
End Sub
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
ChildFrm(0, "Form2")
End Sub
Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
ChildFrm(1, "Form3")
End Sub
Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
ChildFrm(2, "Form4")
End Sub
End Class
我的意思是子窗体只能存在一个,但 Dim f() As Form = {New Form2, New Form3, New Form4}是不是实例化所有的窗体,然后加载在内存中,如果子窗体很多的话是不是很影响速度,并且实例化后的对象是不是一直保存在内存中,.net的垃圾回收机制是怎样的?
改好代码者必有重谢