最佳实践 - 销毁对象 [英] Best Practice - Destroying objects

查看:51
本文介绍了最佳实践 - 销毁对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我追求最佳实践宣告和销毁物品的建议。


一些示例代码:


Private Sub MySub


Dim frmMyForm作为MyForm


尝试


frmMyForm =新MyForm

frmMyForm.DoSomething


Catch ex As Exception

ProcessError(ex)

最后

如果不是frmMyForm什么都没有那么

frmMyForm.Dispose()

frmMyForm =没什么

结束如果

结束尝试

结束子


我在例程的开头声明我的对象(在这种情况下为表格)。

我创建它和我们它。

然后我在Finally部分销毁它。


我使用Try Catch来捕获和处理因使用

表单(或对象)而导致的任何错误。

我销毁Finally部分中的对象,以便代码运行即使

有错误。如果是在主试试部分,它可能无法运行。


但是如果在VB2008中使用逻辑,我会收到警告,说我正在使用

变量在赋值之前。


如何确保我能应对意外错误?

如何声明我的对象?

我是摧毁它们还是简单地让GC做它。?

您的意见请




-

Tim

Hi,
I am after suggestions on the best practice declaring and destroying objects.

some example code:

Private Sub MySub

Dim frmMyForm As MyForm

Try

frmMyForm = New MyForm
frmMyForm.DoSomething

Catch ex As Exception
ProcessError(ex)
Finally
If Not frmMyForm Is Nothing Then
frmMyForm.Dispose()
frmMyForm = Nothing
End If
End Try
End Sub

I declare my object (form in this case) at the beginning of the routine.
I create it and us it.
Then I destroy it in the Finally section.

I use a Try Catch to capture and process any errors resulting from the use
of the form (or object).
I destroy the objects in the Finally section so that the code is run even if
there is an error. If it was in the main Try section it may not be run.

However if the logic is used in VB2008 I get a warning saying I am using a
variable before it is assigned a value.

How do I ensure I cater for unexpected errors?
How do I declare my objects?
Do I destroy them or simple let GC do it.?
Your opinions please



--
Tim

推荐答案

9月12日上午8:40 *,Tim Marsden< tm ... @ newsgroup。 nospamwrote:
On Sep 12, 8:40*am, Tim Marsden <tm...@newsgroup.nospamwrote:



我追求最佳实践宣告和销毁物品的建议。


一些示例代码:


Private Sub MySub


* * Dim frmMyForm作为MyForm *


* *试试


* * * * frmMyForm = New MyForm *

* * * * frmMyForm.DoSomething


* * * Catch ex As Exception

* * * * Proc essError(ex)

* * *最后

* * * *如果不是frmMyForm什么都没有那么

* * * * * frmMyForm.Dispose ()

* * * * * frmMyForm = Nothing

* * * *结束如果

* *结束尝试

结束子


我在例行程序开始时声明我的对象(在这种情况下为表格)。

我创建它并使用它。

然后我在Finally部分销毁它。


我使用Try Catch来捕获和处理因使用而导致的任何错误

形式(或对象)。

我销毁Finally部分中的对象,以便代码运行,即使是
也有错误。如果是在主试试部分,它可能无法运行。


但是如果在VB2008中使用逻辑,我会收到警告,说我正在使用

变量在赋值之前。


如何确保我能应对意外错误?

如何声明我的对象?

我是摧毁它们还是简单地让GC做它。?

您的意见请


-

Tim *
Hi,
I am after suggestions on the best practice declaring and destroying objects.

some example code:

Private Sub MySub

* * Dim frmMyForm As MyForm *

* * Try

* * * *frmMyForm = New MyForm *
* * * *frmMyForm.DoSomething

* * *Catch ex As Exception
* * * *ProcessError(ex)
* * *Finally
* * * *If Not frmMyForm Is Nothing Then
* * * * * frmMyForm.Dispose()
* * * * * frmMyForm = Nothing
* * * *End If
* * End Try
End Sub

I declare my object (form in this case) at the beginning of the routine.
I create it and us it.
Then I destroy it in the Finally section.

I use a Try Catch to capture and process any errors resulting from the use
of the form (or object).
I destroy the objects in the Finally section so that the code is run evenif
there is an error. If it was in the main Try section it may not be run.

However if the logic is used in VB2008 I get a warning saying I am using a
variable before it is assigned a value.

How do I ensure I cater for unexpected errors?
How do I declare my objects?
Do I destroy them or simple let GC do it.?
Your opinions please

--
Tim *



对于IDisposable对象,我建议您每当

就可以处置它们。这个话题是一个很大的争论(Cor将很快在这里说

与我的情况相反)所以我不会理解为什么,但我会

鼓励你搜索档案进行讨论。


在你的例子中,下面的任何一个都应该有效(我更喜欢后者)


//////////////

Dim frmMyForm As MyForm = Nothing

尝试

frmMyForm =新的MyForm

frmMyForm.DoSomething

Catch ex As Exception

ProcessError(ex)

最后

如果不是frmMyForm什么都没有那么

frmMyForm.Dispose()

frmMyForm =没什么

结束如果

结束尝试

//////////////


//////////// //

尝试

使用frmMyForm作为新的MyForm()

frmMyForm.DoSomething()

结束使用

Catch ex As Exception

ProcessError(ex)

结束尝试

///////// /////


谢谢,


Seth Rowe [MVP]
http:// sethrowe.blogspot.com/


9月12日上午7:40 *,Tim Marsden< tm ... @ newsgroup.nospamwrote:
On Sep 12, 7:40*am, Tim Marsden <tm...@newsgroup.nospamwrote:



我在提出关于声明和销毁物品的最佳做法的建议之后。


some示例代码:


Private Sub MySub


* * Dim frmMyForm作为MyForm *


* *试试


* * * * frmMyForm = New MyForm *

* * * * frmMyForm.DoSomething


* * * Catch ex As Exception

* * * * ProcessError(ex)

* * *最后

* * * *如果不是frmMyForm什么都没有呢

* * * * * frmMyForm.Dispose()

* * * * * frmMyForm =没什么

* * * *结束如果

* *结束尝试

结束子
Hi,
I am after suggestions on the best practice declaring and destroying objects.

some example code:

Private Sub MySub

* * Dim frmMyForm As MyForm *

* * Try

* * * *frmMyForm = New MyForm *
* * * *frmMyForm.DoSomething

* * *Catch ex As Exception
* * * *ProcessError(ex)
* * *Finally
* * * *If Not frmMyForm Is Nothing Then
* * * * * frmMyForm.Dispose()
* * * * * frmMyForm = Nothing
* * * *End If
* * End Try
End Sub



设置frmMyForm =这里什么都没有意义sinc变量具有

本地范围。事实上,在该行甚至执行之前,frmMyForm引用的对象符合

的集合。当然,这是

,假设CLR甚至首先执行它,因为它可能是优雅的

Setting frmMyForm = Nothing is pointless here since the variable has
local scope. In fact, the object referenced by frmMyForm is eligible
for collection before that line even executes. Of course, that is
assuming the CLR even executes it in first place since it may be
optimized out.


>

我在例行程序开始时声明我的对象(在这种情况下为表格)。

我创建它和我们它。

然后我在Finally部分销毁它。


我使用Try Catch捕获并处理因使用

表格而导致的任何错误(或对象)。

我销毁Finally部分中的对象,以便代码运行,即使是
也有错误。如果是在主试试部分,它可能无法运行。


但是如果在VB2008中使用逻辑,我会收到警告,说我正在使用

变量在赋值之前。
>
I declare my object (form in this case) at the beginning of the routine.
I create it and us it.
Then I destroy it in the Finally section.

I use a Try Catch to capture and process any errors resulting from the use
of the form (or object).
I destroy the objects in the Finally section so that the code is run evenif
there is an error. If it was in the main Try section it may not be run.

However if the logic is used in VB2008 I get a warning saying I am using a
variable before it is assigned a value.



嗯......我必须仔细检查明确赋值的规则,但

我怀疑编译器正在认识到MyForm的实例化

可以抛出异常,导致在

finally部分中使用该变量,然后才能明确赋值。

Hmm...I''ll have to double check the rules for definite assignment, but
I suspect the compiler is recognizing that the instantiation of MyForm
could throw an exception resulting in a usage of the variable in the
finally section before it is definitely assigned.


>

如何确保我能够应对意外错误?
>
How do I ensure I cater for unexpected errors?



使用''using''构造。

Use the ''using'' construct.


如何声明我的对象?
How do I declare my objects?



Dim frmMyForm作为MyForm =什么都行不通。

Dim frmMyForm As MyForm = Nothing would have worked.


我是摧毁它们还是简单地让GC做到了。?
Do I destroy them or simple let GC do it.?



如果一个对象实现了IDisposable,那么建议你

总是调用Dispose。

If an object implements IDisposable then it is recommened that you
always call Dispose.


您的意见


-

Tim *
Your opinions please

--
Tim *


Tim


最后,只是try catch块的一部分,其中finaly意味着它只要计算机没有拔掉就可以完成



将对象的引用设置为空是绝对没有任何意义在VB6之后VB中的
,这很简单,因为它只执行一条指令

将内存地址设置为全零。该对象仍然保留在托管的

堆中,直到它被垃圾收集器删除。


您无法破坏代码中的任何对象。你使用dispose作为

Idispose是正确的实现释放你的

对象中使用的所有非托管资源(就像那些)。 Dispose是一个来自组件的方法,而20%

的类(特别是控件)继承了它。


但是,通过SharePoint和更多类中的实例,托管代码

只是Com对象的包装器,建议使用dispose来释放那些非托管资源。如果您正在使用SharePoint,那么简单的

调用dispose方法让Idisposable部分完成它的工作。

因此,SharePoint团队称为良好实践调用

处理每个对象的末尾(他们最近更改了该文本,它现在或多或少是
,这是Sharepoint对象的良好做法)。


请注意,从托管代码的第一天开始,dispose被视为解构器的替换。

。在那些日子里(并且在非托管C ++中,但是不是在Java中的
)你应该使用那个解构器,因此是在Java

和其他托管语言如VB现在的垃圾集电极。 (这就是为什么它被称为托管代码的
)。有些人开始进行paroting,

使用Dispose是一种很好的做法,因为方法是在一个类中,同样的

因为它是非托管代码的良好做法来解决每个对象。在互联网上有大量的页面,人们只是在说它是好的

练习,但从不告诉为什么(或者它没有伤害的东西)。 />

使用解构函数可以销毁对象。但是,强烈建议不要这样做并让垃圾收集器完成它的工作。


查看表单的设计器代码(在更多的向导中,你可以看到IDispose的实现,这是在

表单的每一个结束时完成的。如果你自己创建你的表单,那么你必须自己实现这个代码

才能从dispose中受益。如果你使用的对象是
实现非常多的句柄,比如笔,那么每次使用

dispose(释放句柄)之前,每次对象都是

不再引用了(这不仅是它自己的引用,那个

就像我开始时一样,在这种情况下没有)。


Cor


Tim Marsden < tm *** @ newsgroup.nospamschreef in bericht

新闻:B2 *************************** ******* @ microsof t.com ...
Tim

Finaly, is only a part of a try catch block, where finaly means that it
always is done as long as the computer is not unplugged.

Setting the reference of an object to nothing is absolute without any sense
in VB after VB6, simple because it does nothing more then an instruction to
set a memory address to all zeroes. The object still stays at the managed
heap until it is removed by the Garbage Collector.

You are nowhere destroying any object in your code. You use dispose which as
Idispose is right implemtend releases all unmanaged resource used in your
object (as those are there). Dispose is a methode from components, while 20%
of the classes (especially controls) inherits that.

However, by instance in SharePoint and more classes, where the managed code
is only a wrapper around Com objects, it is adviced to use dispose to
release those unmanaged resources. If you are using SharePoint, then simple
invoke the dispose method to let the Idisposable section do its work.
Therefore it is by the SharePoint team called good practise to invoke
dispose at the end of every object (They have changed that text lately, it
is now more or less, good practise for Sharepoint objects).

Be aware that from the first day of managed code the dispose was seen as a
replacement for the deconstructor. In those days (and in non managed C++ but
not in Java) you should have used that deconstructor, therefore is in Java
and other managed languages like VB now the Garbage Collector. (And that is
in fact why it is called managed code). Some people started paroting, that
it was good practise to use Dispose as the method was in a class, the same
as it is with non managed code good practise to descruct every object. There
are tons of pages on Internet where people are only telling that it is good
practise, but never tell why (or something as it does not harm).

To destroy an object is possible by using the deconstructor. However, it is
strongly adviced not to do that and let the Garbage Collector do its work.

Have a look at the designer code of a form (it is in more wizards), there
you see the implementation of the IDispose which is done at every close of a
form. If you create yourform yourself, then you have to implement this code
as well yourself to have benefit from the dispose. If you use objects which
implements very much handles like pens, then it is aviced to use then
dispose (to release the handles) everytime before there the object is
nowhere referenced anymore (and that is not only its own reference, that one
does as I started with, in this case nothing).

Cor

"Tim Marsden" <tm***@newsgroup.nospamschreef in bericht
news:B2**********************************@microsof t.com...



我追求最佳实践宣言的建议并销毁

对象。


一些示例代码:


Private Sub MySub

Dim frmMyForm作为MyForm


尝试


frmMyForm =新的MyForm

frmMyForm.DoSomething


Catch ex As Exception

ProcessError(ex)

最后

如果不是frmMyForm什么都没有那么

frmMyForm.Dispose()

frmMyForm =没什么

结束如果

结束尝试

结束子


我在例行程序开始时声明我的对象(在这种情况下为表格)。

我创建它并使用它。

然后我在Finally部分销毁它。


我使用Try Catch捕获和处理因使用

表单(或对象)而导致的任何错误。

我销毁Finally部分中的对象,以便代码运行甚至

如果

有错误。如果是在主试试部分,它可能无法运行。


但是如果在VB2008中使用逻辑,我会收到警告,说我正在使用

变量在赋值之前。


如何确保我能应对意外错误?

如何声明我的对象?

我是摧毁它们还是简单地让GC做它。?

您的意见请




- < br $>
Tim
Hi,
I am after suggestions on the best practice declaring and destroying
objects.

some example code:

Private Sub MySub

Dim frmMyForm As MyForm

Try

frmMyForm = New MyForm
frmMyForm.DoSomething

Catch ex As Exception
ProcessError(ex)
Finally
If Not frmMyForm Is Nothing Then
frmMyForm.Dispose()
frmMyForm = Nothing
End If
End Try
End Sub

I declare my object (form in this case) at the beginning of the routine.
I create it and us it.
Then I destroy it in the Finally section.

I use a Try Catch to capture and process any errors resulting from the use
of the form (or object).
I destroy the objects in the Finally section so that the code is run even
if
there is an error. If it was in the main Try section it may not be run.

However if the logic is used in VB2008 I get a warning saying I am using a
variable before it is assigned a value.

How do I ensure I cater for unexpected errors?
How do I declare my objects?
Do I destroy them or simple let GC do it.?
Your opinions please



--
Tim


这篇关于最佳实践 - 销毁对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆