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 delete the class?

Do I need to delete the class?

Scheduled Pinned Locked Moved Visual Basic
javaquestion
5 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.
  • I Offline
    I Offline
    IlanTal
    wrote on last edited by
    #1

    I am writing an application which should have a whole bunch of classes. Depending upon the choices of the user, some of the classes will be actually used and others will not be used. Now I'm worried about garbage collection, since Visual Basic isn't Java. Is my code below mistaken in that I need to call delete? Private Sub Test1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Test1.Click Dim myScope As ScopeClass = New ScopeClass Dim stat As Integer stat = myScope.Init(Me) stat = myScope.PerformScan(1) myScope.Close() End Sub If I do call a series of classes and I go from one class to another, is there an easy way to have a pointer to the last class I used so that I don't have to know specifically what it was at the point when I want to delete it? If I can do this, please suggest a code snippet with the proper syntax to do it. Thanks, Ilan

    D D 2 Replies Last reply
    0
    • I IlanTal

      I am writing an application which should have a whole bunch of classes. Depending upon the choices of the user, some of the classes will be actually used and others will not be used. Now I'm worried about garbage collection, since Visual Basic isn't Java. Is my code below mistaken in that I need to call delete? Private Sub Test1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Test1.Click Dim myScope As ScopeClass = New ScopeClass Dim stat As Integer stat = myScope.Init(Me) stat = myScope.PerformScan(1) myScope.Close() End Sub If I do call a series of classes and I go from one class to another, is there an easy way to have a pointer to the last class I used so that I don't have to know specifically what it was at the point when I want to delete it? If I can do this, please suggest a code snippet with the proper syntax to do it. Thanks, Ilan

      D Offline
      D Offline
      Dave Sexton
      wrote on last edited by
      #2

      IlanTal wrote:

      Now I'm worried about garbage collection, since Visual Basic isn't Java.

      The .NET framework has automatic garbage collection and memory management. You can also use the Dispose() and Finalize() methods of classes. Suggestion for your code - declare your class and counter variables at modular level and instantiate the class when your chosen event is fired e.g.

      Option Strict On

      ....

      'module level class declaration
      Dim myScope as ScopeClass

      'module level counter variable
      Dim stat as integer

      Private Sub Test1_Click(....) Handles Test1.Click

      myScope = New ScopeClass
      stat +=1

      ....

      End Sub

      Remember to decrement your counter variable if/when you dispose the class (stat -=1).

      I 1 Reply Last reply
      0
      • I IlanTal

        I am writing an application which should have a whole bunch of classes. Depending upon the choices of the user, some of the classes will be actually used and others will not be used. Now I'm worried about garbage collection, since Visual Basic isn't Java. Is my code below mistaken in that I need to call delete? Private Sub Test1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Test1.Click Dim myScope As ScopeClass = New ScopeClass Dim stat As Integer stat = myScope.Init(Me) stat = myScope.PerformScan(1) myScope.Close() End Sub If I do call a series of classes and I go from one class to another, is there an easy way to have a pointer to the last class I used so that I don't have to know specifically what it was at the point when I want to delete it? If I can do this, please suggest a code snippet with the proper syntax to do it. Thanks, Ilan

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        IlanTal wrote:

        what it was at the point when I want to delete it?

        Delete it?? You don't have to delete it. If the instance goes out of scope and there are no references to it, it gets killed automatically.

        Dave Kreskowiak Microsoft MVP - Visual Basic

        1 Reply Last reply
        0
        • D Dave Sexton

          IlanTal wrote:

          Now I'm worried about garbage collection, since Visual Basic isn't Java.

          The .NET framework has automatic garbage collection and memory management. You can also use the Dispose() and Finalize() methods of classes. Suggestion for your code - declare your class and counter variables at modular level and instantiate the class when your chosen event is fired e.g.

          Option Strict On

          ....

          'module level class declaration
          Dim myScope as ScopeClass

          'module level counter variable
          Dim stat as integer

          Private Sub Test1_Click(....) Handles Test1.Click

          myScope = New ScopeClass
          stat +=1

          ....

          End Sub

          Remember to decrement your counter variable if/when you dispose the class (stat -=1).

          I Offline
          I Offline
          IlanTal
          wrote on last edited by
          #4

          Thanks for your suggestions. Question: My Test1_Click is a button, so every time I press the button I get a new instance of the class? (According to the code it should be so.) When it goes out of scope does the instance hang around in memory? It seems your use of stat is a counter, but I'm only incrementing it each time I click the button. Should I use Dispose() at the end of the button routine? Is this something I need to consider, or can I just forget it like in Java?

          D 1 Reply Last reply
          0
          • I IlanTal

            Thanks for your suggestions. Question: My Test1_Click is a button, so every time I press the button I get a new instance of the class? (According to the code it should be so.) When it goes out of scope does the instance hang around in memory? It seems your use of stat is a counter, but I'm only incrementing it each time I click the button. Should I use Dispose() at the end of the button routine? Is this something I need to consider, or can I just forget it like in Java?

            D Offline
            D Offline
            Dave Sexton
            wrote on last edited by
            #5

            IlanTal wrote:

            When it goes out of scope does the instance hang around in memory?

            No, if it goes out of scope the instance is no more and the memory is reclaimed.

            IlanTal wrote:

            Should I use Dispose() at the end of the button routine?

            You can call the Dispose() method when you no longer need your class. If you call it at the end of your button routine you'll throw a null reference exception if you try to make use of the class later in another sub as you cannot access a disposed object. A basic use of the Dispose() method in your click event would be something like

            Option Strict On
            Public Class Form1

            Dim myScope as ScopeClass 'declare the class

            Private Sub Test_Click(...)

            'create new ScopeClass instance
            myScope = New ScopeClass

            'do whatever the ScopeClass needs to do
            'call other methods/functions etc
            ......

            'ScopeClass is no longer needed,
            'all processing that needed to be done is complete
            myScope.Dispose()

            End Sub
            End Class

            Because of the automatic garbage collection of the .NET framework you don't HAVE to call Dispose() but it can be used to manually reclaim memory.

            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