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. How to dispose of a particular instance of a class?

How to dispose of a particular instance of a class?

Scheduled Pinned Locked Moved Visual Basic
questiontutorial
7 Posts 3 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.
  • S Offline
    S Offline
    Sonhospa
    wrote on last edited by
    #1

    Hello, this might be a dummy question, but I couldn't find a good answer on the internet yet. In my code I populate a CheckedListBox with the filenames of a particular filetype. If one of the entries gets checked ('ItemCheck' event of the CheckedListBox), I create an instance of a class that represents one of the relevant files.

    	If ctrl.GetItemCheckState(ctrl.SelectedIndex) = 0 Then
    		' create a new cfgFile class AND add it to the classes list
    		ff = New cfgFile(ctrl.SelectedItem)
    		lbl\_FileMessage.Text &= ff.Message
    	Else
            ' How to dispose of the correct instance?
        End if
    

    How can I properly dispose of the correct classes' instance, once the item is unchecked again? I thought about looping through the instances and check for their 'filename' property... but there doesn't seem to be a collection of instances, which I can refer to in a loop. Thank you for your advice, Mick

    M 1 Reply Last reply
    0
    • S Sonhospa

      Hello, this might be a dummy question, but I couldn't find a good answer on the internet yet. In my code I populate a CheckedListBox with the filenames of a particular filetype. If one of the entries gets checked ('ItemCheck' event of the CheckedListBox), I create an instance of a class that represents one of the relevant files.

      	If ctrl.GetItemCheckState(ctrl.SelectedIndex) = 0 Then
      		' create a new cfgFile class AND add it to the classes list
      		ff = New cfgFile(ctrl.SelectedItem)
      		lbl\_FileMessage.Text &= ff.Message
      	Else
              ' How to dispose of the correct instance?
          End if
      

      How can I properly dispose of the correct classes' instance, once the item is unchecked again? I thought about looping through the instances and check for their 'filename' property... but there doesn't seem to be a collection of instances, which I can refer to in a loop. Thank you for your advice, Mick

      M Offline
      M Offline
      Michael_Davies
      wrote on last edited by
      #2

      Where are you holding the created instances and how are they referenced to a particular line in the list box?

      S 2 Replies Last reply
      0
      • M Michael_Davies

        Where are you holding the created instances and how are they referenced to a particular line in the list box?

        S Offline
        S Offline
        Sonhospa
        wrote on last edited by
        #3

        Hi Michael, I guess that's part of my question... I tried to put every newly created class into a List(of class), where I could add/remove entries. But removing elements from the list (i.e. list.count resulted in -1) didn't dispose of their instances (static counter variable, decremented inside the 'dispose' method, stayed the same). Thank you for guiding me further.

        1 Reply Last reply
        0
        • M Michael_Davies

          Where are you holding the created instances and how are they referenced to a particular line in the list box?

          S Offline
          S Offline
          Sonhospa
          wrote on last edited by
          #4

          Would that be a probable solution?

          Public Class cfgFiles
          Inherits SortedList(Of String, cfgFile)

          Private Shared files As New SortedList(Of String, cfgFile)
          
          Public Overloads Shared Sub Add(ByVal fileName As String)
          	files.Add(fileName, New cfgFile(fileName))
          End Sub
          
          Public Overloads Shared Sub Remove(ByVal fileName As String)
          	Dim ff As cfgFile = files(fileName)
          	ff.Dispose()
          	files.Remove(fileName)
          End Sub
          
          Public Overloads Function GetEnumerator() As IEnumerator(Of cfgFile)
          	Return files.GetEnumerator
          End Function
          
          Public Property SelectedItem As cfgFile
          Default Public Overloads Property Item(index As Integer)
          	Get
          		SelectedItem = files(index)
          		Return SelectedItem
          	End Get
          	Set(value)	' as cfgFile?
          		files(index) = value
          	End Set
          End Property
          

          End Class

          L 1 Reply Last reply
          0
          • S Sonhospa

            Would that be a probable solution?

            Public Class cfgFiles
            Inherits SortedList(Of String, cfgFile)

            Private Shared files As New SortedList(Of String, cfgFile)
            
            Public Overloads Shared Sub Add(ByVal fileName As String)
            	files.Add(fileName, New cfgFile(fileName))
            End Sub
            
            Public Overloads Shared Sub Remove(ByVal fileName As String)
            	Dim ff As cfgFile = files(fileName)
            	ff.Dispose()
            	files.Remove(fileName)
            End Sub
            
            Public Overloads Function GetEnumerator() As IEnumerator(Of cfgFile)
            	Return files.GetEnumerator
            End Function
            
            Public Property SelectedItem As cfgFile
            Default Public Overloads Property Item(index As Integer)
            	Get
            		SelectedItem = files(index)
            		Return SelectedItem
            	End Get
            	Set(value)	' as cfgFile?
            		files(index) = value
            	End Set
            End Property
            

            End Class

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Memory allocated in .NET using the new command can automatically claimed by the GC as soon as the object loses scope. I say can, because it won't do it immediatly. Disposing is only required if there are unmanaged resources owned by the object. Which means you can safely remove the "ff.Dispose()" statement. Also see Object.Finalize Method (System)[^], Garbage Collection[^] and Implementing a Dispose Method[^].

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

            S 1 Reply Last reply
            0
            • L Lost User

              Memory allocated in .NET using the new command can automatically claimed by the GC as soon as the object loses scope. I say can, because it won't do it immediatly. Disposing is only required if there are unmanaged resources owned by the object. Which means you can safely remove the "ff.Dispose()" statement. Also see Object.Finalize Method (System)[^], Garbage Collection[^] and Implementing a Dispose Method[^].

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

              S Offline
              S Offline
              Sonhospa
              wrote on last edited by
              #6

              Thank you for this insight, Eddy. Still I am confused having the list.count variable reduced after removing the list entry (which is an instance), but the instance itself seems to be still alive (shared counter variable of the class doesn't decrement).

              L 1 Reply Last reply
              0
              • S Sonhospa

                Thank you for this insight, Eddy. Still I am confused having the list.count variable reduced after removing the list entry (which is an instance), but the instance itself seems to be still alive (shared counter variable of the class doesn't decrement).

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                That is because it still is alive. It will remain so until the GC collects it. You can't "force" it to be deleted from memory.

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                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