处置类 vb.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19097555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dispose class vb.net
提问by Mokmeuh
I see alot of "Tutorial" on how to dispose a class but I can't understand plus all of them are explain in c# not in vb.net it's quite similar I know but it seems I can,t get it and I don't know why
我看到很多关于如何处理类的“教程”,但我无法理解,而且所有这些都是在 c# 中而不是在 vb.net 中解释的,我知道它非常相似,但似乎我无法理解,但我不明白不知道为什么
So my question : How I can implement a IDisposable in this class
所以我的问题:我如何在这个类中实现 IDisposable
Public Class Container
Private _sItemName As String
Private _sPrice As Single
Private _sPriceTot As Single
Private _iNumber As Integer
Sub New(ByVal _sItemName As String, ByVal _sPrice As Single, ByVal _sPriceTot As Single, ByVal _iNumber As Integer)
Me._sItemName = _sItemName
Me._sPrice = _sPrice
Me._iNumber = _iNumber
Me._sPriceTot = _sPriceTot
End Sub
Public Property sItemName() As String
Get
Return _sItemName
End Get
Private Set(value As String)
_sItemName = value
End Set
End Property
Public Property sPriceTot() As Single
Get
Return _sPriceTot
End Get
Private Set(value As Single)
_sPriceTot = value
End Set
End Property
Public Property sPrice() As Single
Get
Return _sPrice
End Get
Private Set(value As Single)
_sPrice = value
End Set
End Property
Public Property iNumber() As String
Get
Return _iNumber
End Get
Private Set(value As String)
_iNumber = value
End Set
End Property
End Class
Before someone ask what I'm trying to do is to do the same as the .delete in c++
在有人问我想要做什么之前,先做与 C++ 中的 .delete 相同的事情
回答by Matt Wilko
Specify that your class implement IDisposable by adding the following:
通过添加以下内容,指定您的类实现 IDisposable:
Public Class Container : Implements IDisposable
Press Enter and the Dispose Methods are added automatically for you:
按 Enter 会自动为您添加 Dispose 方法:
#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
' TODO: dispose managed state (managed objects).
End If
' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
' TODO: set large fields to null.
End If
Me.disposedValue = True
End Sub
' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
'Protected Overrides Sub Finalize()
' ' Do not change this code. Put cleanup code in Dispose(ByVal disposing As Boolean) above.
' Dispose(False)
' MyBase.Finalize()
'End Sub
' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above.
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
Then just add the appropriate code in the TODO sections
然后只需在 TODO 部分添加适当的代码
回答by supercat
Typically, a class will implements IDisposableif instances of that class will--or are likely to--know of some action that should be performed sometime within the lifetime of the universe, and also know that they are likely the only things with the knowledge and impetus necessary to perform those actions. Calling Disposeis a way of telling such a class instance "if you want to make sure something gets done, here's your last chance".
通常,IDisposable如果该类的实例将 - 或可能 - 知道应该在宇宙生命周期内的某个时间执行的某些操作,并且还知道它们可能是唯一具有知识和执行这些操作所需的动力。调用Dispose是一种告诉这样的类实例“如果你想确保某事完成,这是你最后的机会”的一种方式。
The most common usage of this pattern occurs when a class asks some outside entity to do something on its behalf, to the detriment of other objects or entities, until further notice [e.g. a class encapsulating a file may ask the underlying OS for exclusive access to a file, to the detriment of any other program that might want to use it]. Calling Disposeon the class will cause it to notify the file system that it no longer needs the file, and it may thus be made available to other entities.
这种模式最常见的用法是当一个类要求某个外部实体代表它做某事时,这会损害其他对象或实体,直到另行通知[例如,封装文件的类可能会要求底层操作系统独占访问一个文件,从而损害可能想要使用它的任何其他程序]。调用Dispose该类将导致它通知文件系统它不再需要该文件,因此它可能可供其他实体使用。
In general, if an object which implements IDisposableis abandoned without calling Dispose, some things which should get done, won't be. There is a mechanism in .NET by which objects can request notification if the system notices that they have been abandoned, and some objects make use of this mechanism as a "fallback" in case they are abandoned without being properly disposed. There are some pretty severe limitations with this mechanism, however; one should never rely upon it unless one has particular detailed knowledge of exactly how it will behave in one's particular case.
一般来说,如果一个实现的对象IDisposable没有调用就被放弃Dispose,那么一些应该完成的事情就不会完成。.NET 中有一种机制,如果系统注意到它们已被放弃,对象可以通过该机制请求通知,并且某些对象利用此机制作为“后备”,以防它们在没有正确处置的情况下被放弃。然而,这种机制有一些非常严重的限制;除非人们对它在特定情况下的行为方式有特别详细的了解,否则永远不应依赖它。

