Dispose class vb.net

泄露秘密 提交于 2020-12-06 04:34:12

问题


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++


回答1:


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




回答2:


Typically, a class will implements IDisposable if instances of that class will--or are likely to--know of some action that should be performed sometime within the lifetime of the universe, and also know that they are likely the only things with the knowledge and impetus necessary to perform those actions. Calling Dispose is a way of telling such a class instance "if you want to make sure something gets done, here's your last chance".

The most common usage of this pattern occurs when a class asks some outside entity to do something on its behalf, to the detriment of other objects or entities, until further notice [e.g. a class encapsulating a file may ask the underlying OS for exclusive access to a file, to the detriment of any other program that might want to use it]. Calling Dispose on the class will cause it to notify the file system that it no longer needs the file, and it may thus be made available to other entities.

In general, if an object which implements IDisposable is abandoned without calling Dispose, some things which should get done, won't be. There is a mechanism in .NET by which objects can request notification if the system notices that they have been abandoned, and some objects make use of this mechanism as a "fallback" in case they are abandoned without being properly disposed. There are some pretty severe limitations with this mechanism, however; one should never rely upon it unless one has particular detailed knowledge of exactly how it will behave in one's particular case.



来源:https://stackoverflow.com/questions/19097555/dispose-class-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!