How to Write a Class Dispose with Visual Basic

104 8
    • 1). Open your .VB file in Visual Basic .NET.

    • 2). Add an "Implements IDisposable" declaration at the top of your class. For example, if your class is named "MyDisposableThing", it should look like this:

      Public Class MyDisposableThing
      Implements IDisposable
      ' Your class's implementation...
      End Class

    • 3). Add a Dispose method to your class, between the "Implements" declaration and the "End Class" line. It should look like this:

      Public Sub Dispose() Implements IDisposable.Dispose
      ' Disposal code
      End Sub

    • 4). Add code to properly dispose of any un-garbage-collected resources your class uses. This will depend on the specifics of your class. If any of the members of your class also implement IDisposable, you should probably call their Dispose method (check their class documentation to make sure). For example:

      myDisposableMember.Dispose()

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.