我看到了很多有关如何布置类的“教程”,但我听不懂,而且所有这些都是在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,将自动为您添加“处置方法”:
#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部分中添加适当的代码
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句