处置类vb.net [英] Dispose class vb.net
问题描述
我看到了很多关于如何布置类"的教程",但是我不明白,而且所有这些都是在c#中解释的,而不是在vb.net中,它与我所知道的非常相似,但是似乎我无法理解,并且我不知道为什么
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
所以我的问题:我如何在此类中实现IDisposable
So my question : How I can implement a IDisposable in this class
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相同的操作
Before someone ask what I'm trying to do is to do the same as the .delete in c++
推荐答案
通过添加以下内容来指定您的类实现IDisposable:
Specify that your class implement IDisposable by adding the following:
Public Class Container : Implements IDisposable
按Enter键,将自动为您添加处置方法":
Press Enter and the Dispose Methods are added automatically for you:
#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部分中添加适当的代码
Then just add the appropriate code in the TODO sections
这篇关于处置类vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!