Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dispose class 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

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

Before someone ask what I'm trying to do is to do the same as the .delete in c++

like image 419
Mokmeuh Avatar asked Oct 23 '25 18:10

Mokmeuh


1 Answers

Specify that your class implement IDisposable by adding the following:

Public Class Container : Implements IDisposable

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

Then just add the appropriate code in the TODO sections

like image 98
Matt Wilko Avatar answered Oct 26 '25 08:10

Matt Wilko