Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Do I need to implement Dispose() ?

Do I need to implement Dispose() ?

Scheduled Pinned Locked Moved Visual Basic
databasequestionannouncement
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Mujica
    wrote on last edited by
    #1

    I have created a class which when instantiated, creates a SQL connection, SQL command, etc. By repeatedly calling some methods it processes data using that one connection and SQL command. When I'm done with this object, shouldn't I close/dispose of my SQL connection and command? Should I do this by implementing Dispose() ? Or Should I create a method within my class called,"CleanUp" and just release the SQL connection and command there. Also, I have a few Dictionaries and Lists in this class; should I ".Clear" them also ? Maybe I should just set "myObject = Nothing" in my calling application? Your guidance is appreciated.

    Richard DeemingR 1 Reply Last reply
    0
    • D David Mujica

      I have created a class which when instantiated, creates a SQL connection, SQL command, etc. By repeatedly calling some methods it processes data using that one connection and SQL command. When I'm done with this object, shouldn't I close/dispose of my SQL connection and command? Should I do this by implementing Dispose() ? Or Should I create a method within my class called,"CleanUp" and just release the SQL connection and command there. Also, I have a few Dictionaries and Lists in this class; should I ".Clear" them also ? Maybe I should just set "myObject = Nothing" in my calling application? Your guidance is appreciated.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      If your class owns references to disposable objects, it should implement IDisposable to dispose of those references. Providing a public method with a different name has no benefit; you won't be able to wrap your class in a Using block, and developers using your class might not even realise that they have to call your SpecialSnowflakeCleanUp method when they've finished with an instance of your class. If your class implements IDispoable, it's a clear indication that it requires clean-up. There's no need to clear collections or references to managed objects; you only need to worry about unmanaged references and disposable objects. Implementing IDisposable and the Dispose Pattern Properly[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      D 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        If your class owns references to disposable objects, it should implement IDisposable to dispose of those references. Providing a public method with a different name has no benefit; you won't be able to wrap your class in a Using block, and developers using your class might not even realise that they have to call your SpecialSnowflakeCleanUp method when they've finished with an instance of your class. If your class implements IDispoable, it's a clear indication that it requires clean-up. There's no need to clear collections or references to managed objects; you only need to worry about unmanaged references and disposable objects. Implementing IDisposable and the Dispose Pattern Properly[^]


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        D Offline
        D Offline
        David Mujica
        wrote on last edited by
        #3

        Hmmm ... Based on your suggestions, I am going to refactory my code.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups