我看到很多关于如何处理类的“教程”,但我无法理解,而且所有这些都是在 c# 中而不是在 vb.net 中解释的,我知道它非常相似,但似乎我无法理解,但我不明白不知道为什么
所以我的问题:我如何在这个类中实现 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
在有人问我想要做什么之前,先做与 C++ 中的 .delete 相同的事情
最佳答案
通过添加以下内容,指定您的类实现 IDisposable:
Public Class Container : Implements IDisposable
按 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
然后只需在 TODO 部分添加适当的代码
关于.net - 处置类 vb.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19097555/