构造函数和垃圾回收 高分重谢

taofirst 2003-11-06 09:23:46
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的垃圾回收机制是怎样的?
改好代码者必有重谢
...全文
33 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
taofirst 2003-11-08
  • 打赏
  • 举报
回复
我实在是想让我的100分花的有点价值
poni 2003-11-08
  • 打赏
  • 举报
回复
Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
If Me.DetectChildForm("Form2", Me) Is Nothing Then '调用上面的函数
Dim frm As New Form2()
frm.MdiParent = Me
frm.Show()
End If
End Sub
记得给分哟!
poni 2003-11-08
  • 打赏
  • 举报
回复
Public Function DetectChildForm(ByVal tString As String, ByVal tMDIForm As Form) As System.Windows.Forms.Form
Dim iCounter As Integer
Dim MyForm As New Form()
MyForm = tMDIForm

If MyForm.MdiChildren.Length = 0 Then '如果子窗口数量为0
DetectChildForm = Nothing
Exit Function
End If

For iCounter = 0 To MyForm.MdiChildren.Length - 1
If MyForm.MdiChildren(iCounter).GetType.ToString = Application.ProductName & "." & tString Then
MyForm.MdiChildren(iCounter).Activate()
Return (MyForm.MdiChildren(iCounter))
End If
Next
End Function
NoReady 2003-11-06
  • 打赏
  • 举报
回复
实例化后的对象只要没有去显式的dispose它的话,它实际上还在内存中的,如果类提供了finalize(析构)方法的话,那么GC会去掉它的强引用,但并没有把它从内存中去除,就是说它还一直保存在内存中,一般的说,要GC最起码 2 次COLLECT后,才有可能从内存中移除的。而GC会产生COLLECT动作只有在少数情况下才会发生:如物理内存不足或应用程序退出等。
taofirst 2003-11-06
  • 打赏
  • 举报
回复
我现在用的就是这种方法,可是我想试试其它的,改改我的代码,不过放心,分是有的
dimtxw 2003-11-06
  • 打赏
  • 举报
回复
dim frm2,frm3,frm4 as Form

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
If frm2 Is Nothing Then
frm2=New Form2()
End If
frm2.Show()
frm2.Activate()
End Sub

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
If frm3 Is Nothing Then
frm3=New Form2()
End If
frm3.Show()
frm3.Activate()
End Sub

Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
If frm4 Is Nothing Then
frm4=New Form2()
End If
frm4.Show()
frm4.Activate()
End Sub
End Class

16,556

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧