何时设置对象引用=什么都没有 [英] When to set object references = nothing
问题描述
大家好,
当你完成对象引用时,人们认为最佳做法是什么?我过去曾与之合作的一些人建议,如果你创建一个带有New的对象。那么当你完成它时你应该释放/设置为空,无论它是否即将超出范围。
在一个简单的例子中:
Private Sub DoSomething()
Dim fForm As new frmMain
fForm.Show()
'' ...用这个表格做一些事情
fForm.Close()
fForm = Nothing
End Sub
>
是fForm = Nothing如果垃圾收集将在程序完成后处理对象引用,那么是否必要?这里最好的做法是什么?
谢谢!
Mike
Hi all,
What do people regard as the best practice with respect to freeing object references when you''re done with them? Some people I''ve worked with in the past suggested that if you create an object with "New" then you should free/set it to nothing when you''re finished with it, regardless of whether it''s about to go out of scope or not.
In a simple example:
Private Sub DoSomething()
Dim fForm As New frmMain
fForm.Show()
''... do some things with this form
fForm.Close()
fForm = Nothing
End Sub
Is the "fForm = Nothing" necessary if garbage collection will take care of the object reference once the procedure completes? What is the best practice here?
Thanks!
Mike
推荐答案
你不需要把它设置为零。当DoSomething存在时,其中定义的所有
变量都超出范围并有资格获得垃圾
集合。
" ;迈克伊顿 <弥******* @ discussions.microsoft.com>在留言中写道
新闻:6F ********************************** @ microsof t.com ...
You don''t need to set it to nothing. When DoSomething exists, all the
variables defined in it go out of scope and become eligible for garbage
collection.
"Mike Eaton" <Mi*******@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
大家好,
当你完成对象时,人们认为关于释放对象
引用的最佳做法是什么?他们?我在
过去的一些人曾经建议,如果你创建一个带有New的对象。那么当你完成它时,你应该免费/ b $ b b / no,无论是否b $ b $即将超出范围。
在一个简单的例子中:
Private Sub DoSomething()
Dim fForm as new frmMain
fForm.Show()
''...做一些事情用这个表格
fForm.Close()
fForm = Nothing
End Sub
是fForm = Nothing如果垃圾收集在程序完成后会处理对象引用的
,那么这是必要的吗?什么是最好的
练习呢?
谢谢!
迈克
Hi all,
What do people regard as the best practice with respect to freeing object references when you''re done with them? Some people I''ve worked with in the
past suggested that if you create an object with "New" then you should
free/set it to nothing when you''re finished with it, regardless of whether
it''s about to go out of scope or not.
In a simple example:
Private Sub DoSomething()
Dim fForm As New frmMain
fForm.Show()
''... do some things with this form
fForm.Close()
fForm = Nothing
End Sub
Is the "fForm = Nothing" necessary if garbage collection will take care of the object reference once the procedure completes? What is the best
practice here?
Thanks!
Mike
我的意思是,DoSomething退出,不存在...
" Marina" <所以***** @ nospam.com>在留言中写道
news:el ************** @ TK2MSFTNGP09.phx.gbl ...
I meant, when DoSomething exits, not exists...
"Marina" <so*****@nospam.com> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
你不要需要把它设置为空。当DoSomething存在时,其中定义的所有变量都超出范围,并有资格获得垃圾收集。
Mike Eaton <弥******* @ discussions.microsoft.com>在消息中写道
新闻:6F ********************************** @ microsof t.com。 ..
You don''t need to set it to nothing. When DoSomething exists, all the
variables defined in it go out of scope and become eligible for garbage
collection.
"Mike Eaton" <Mi*******@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
大家好,
当你完成任务时,人们认为最好的做法是释放
Hi all,
What do people regard as the best practice with respect to freeing
对象引用他们?我曾与
合作过的人建议,如果你创建一个带有New的对象。然后,当你完成它时,你应该自由/设置为空,无论它是否即将超出范围。
object references when you''re done with them? Some people I''ve worked with in
the past suggested that if you create an object with "New" then you should
free/set it to nothing when you''re finished with it, regardless of whether
it''s about to go out of scope or not.
在一个简单的例子中:
Private Sub DoSomething()
Dim fForm as new frmMain
fForm.Show()
''...做一些事情用这个表格
fForm.Close()
fForm = Nothing
End Sub
是fForm = Nothing一旦程序完成,垃圾收集是否需要注意
In a simple example:
Private Sub DoSomething()
Dim fForm As New frmMain
fForm.Show()
''... do some things with this form
fForm.Close()
fForm = Nothing
End Sub
Is the "fForm = Nothing" necessary if garbage collection will take care
对象引用?这里有什么最好的练习?
of the object reference once the procedure completes? What is the best
practice here?
谢谢!
Mike
Thanks!
Mike
如果你每次使用相同的对象变量使用
重复创建对象的新引用怎么办?例如:
Dim lstStuff AS ListViewItem = New ListViewItem
lstStuff.Text =" First ListviewItem"
listview1.Items .Add(lstStuff)
lstStuff = Nothing
Dim lstStuff AS ListViewItem = New ListViewItem
lstStuff.Text =" Another ListviewItem"
listview1.Items.Add(lstStuff)
lstStuff = Nothing
是否有必要将lstStuff设置为空创建一个新参考?
" Marina" <所以***** @ nospam.com>在留言中写道
news:el ************** @ TK2MSFTNGP09.phx.gbl ...
What about if you are repeatedly creating a new reference to an object using
the same object variable each time? For example:
Dim lstStuff AS ListViewItem = New ListViewItem
lstStuff.Text = "First ListviewItem"
listview1.Items.Add(lstStuff)
lstStuff = Nothing
Dim lstStuff AS ListViewItem = New ListViewItem
lstStuff.Text = "Another ListviewItem"
listview1.Items.Add(lstStuff)
lstStuff = Nothing
Is it necessary to set lstStuff to nothing before creating a New reference?
"Marina" <so*****@nospam.com> wrote in message
news:el**************@TK2MSFTNGP09.phx.gbl...
你不要需要把它设置为空。当DoSomething存在时,其中定义的所有变量都超出范围,并有资格获得垃圾收集。
Mike Eaton <弥******* @ discussions.microsoft.com>在消息中写道
新闻:6F ********************************** @ microsof t.com。 ..
You don''t need to set it to nothing. When DoSomething exists, all the
variables defined in it go out of scope and become eligible for garbage
collection.
"Mike Eaton" <Mi*******@discussions.microsoft.com> wrote in message
news:6F**********************************@microsof t.com...
大家好,
当你完成任务时,人们认为最好的做法是释放
Hi all,
What do people regard as the best practice with respect to freeing
对象引用他们?我曾与
合作过的人建议,如果你创建一个带有New的对象。然后,当你完成它时,你应该自由/设置为空,无论它是否即将超出范围。
object references when you''re done with them? Some people I''ve worked with in
the past suggested that if you create an object with "New" then you should
free/set it to nothing when you''re finished with it, regardless of whether
it''s about to go out of scope or not.
在一个简单的例子中:
Private Sub DoSomething()
Dim fForm as new frmMain
fForm.Show()
''...做一些事情用这个表格
fForm.Close()
fForm = Nothing
End Sub
是fForm = Nothing一旦程序完成,垃圾收集是否需要注意
In a simple example:
Private Sub DoSomething()
Dim fForm As New frmMain
fForm.Show()
''... do some things with this form
fForm.Close()
fForm = Nothing
End Sub
Is the "fForm = Nothing" necessary if garbage collection will take care
对象引用?这里有什么最好的练习?
of the object reference once the procedure completes? What is the best
practice here?
谢谢!
Mike
Thanks!
Mike
这篇关于何时设置对象引用=什么都没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!