.net - 处置类 vb.net

标签 .net vb.net idisposable

我看到很多关于如何处理类的“教程”,但我无法理解,而且所有这些都是在 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/

相关文章:

c# - 命名空间 'DataSetExtensions' 中不存在类型或命名空间名称 'System.Data'(是否缺少程序集引用?)

c# - 如果文本框为空白或空,如何将文本框的值设置为 "0"?

c# - 检查列表中的所有项目是否相同

json - Newtonsoft JSON - 如何反序列化自定义集合?

c# - 如何从一个方法返回一个流,知道它应该被释放?

c# - Autofac、IDisposable 和在某些情况下手动调用 .Resolve

c# - 无法正确加载底层压缩例程

c# - IEnumerator 返回记录

asp.net-mvc - 将空值添加到 ASP.net MVC 中的 DropDownList

c# - 会为匿名变量调用 dispose 吗?