Results 1 to 7 of 7

Thread: Init Class/Destroy Class

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    182

    Init Class/Destroy Class

    First thanks to everyone for you help. Much appreciated.

    I have nearly all my issues worked out for the conversion from VBA to VB.

    Question: I need to initiate/destroy my classes, but I'm not sure where this is to be performed. Is there an entry and exit function that I can place the initiator/destructor into?

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,852

    Re: Init Class/Destroy Class

    The Sub New is the closest to the idea of an Initialiser.

    Destroying a class instance really depends on what the class does. This might be done with a finalizer / IDisposable - or it but not be needed at all.

    If you can give a bit more detail about what you need to do, then we could probably give more specific details. Otherwise

    https://learn.microsoft.com/en-us/do...define-a-class gives a little bit on Sub New

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    182

    Re: Init Class/Destroy Class

    Quote Originally Posted by PlausiblyDamp View Post
    The Sub New is the closest to the idea of an Initialiser.

    Destroying a class instance really depends on what the class does. This might be done with a finalizer / IDisposable - or it but not be needed at all.

    If you can give a bit more detail about what you need to do, then we could probably give more specific details. Otherwise

    https://learn.microsoft.com/en-us/do...define-a-class gives a little bit on Sub New
    Thanks!!

    Just trying to initialize my classes for use during the life of the program, will use sub new for initialization but still need a way to destory the classes. Then when the program closes I wish to free the memory (destroy the class). I have 2 classes and the form that needs to be ran together. 1 is for the interface to the browser class for dispatching the commands to the browser, the next is the main program and handles the main calls to the browser, then the Form which handles the GUI operation.

    Hope this helps?

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,502

    Re: Init Class/Destroy Class

    Quote Originally Posted by FunkMonkey View Post
    Thanks!!

    Just trying to initialize my classes for use during the life of the program, will use sub new for initialization but still need a way to destory the classes. Then when the program closes I wish to free the memory (destroy the class).
    You don't have to do anything, that's all handled automatically.

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    12,084

    Re: Init Class/Destroy Class

    Not only is that behavior implemented for you, but if the class is no longer needed then it could free up the memory before then.

    Take for example a class that is initialized in a form that is displayed using ShowDialog. As soon as the form is disposed, there's no need for that class any longer and so the garbage collector will probably free up that memory shortly there after.

    The exception to this is when you have a class that implements IDisposable, like a form. The best practice is to initialize the class using the Using statement, e.g.:
    Code:
    Using myDialogForm As New SomeForm()
        If (myDialogForm.ShowDialog() = DialogResult.Ok) Then
            ' do something
        End If
    End Using ' form is disposed
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,796

    Re: Init Class/Destroy Class

    I'm not sure that the memory will be collected quickly. Memory is cheap and plentiful, these days, whereas recovering memory is relatively costly (in time). For that reason, if the program doesn't need to recover the memory, it simply won't. The object will sit there until the program is closed, and that's a good way to manage it. However, you almost never have to deal with recovering memory yourself.

    If there is a Dispose method, then it is good form to call it when the class is no longer needed. If there's a Dispose method in a class that you didn't create, then you may not know what it is doing. It may be doing nothing, or at least nothing important. On the other hand, the class could be holding some kind of system resource, in which case calling Dispose will release that resource. If you wrote the class, then you know what it is holding, and most classes are holding nothing other than the memory they occupy, in which case calling Dispose on that is kind of pointless.
    My usual boring signature: Nothing

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,066

    Re: Init Class/Destroy Class

    You should probably do some reading on .NET garbage collection. That is an automated system that frees up resources, particularly memory, as and when needed. It means writing much less code for cleaning up objects and it also makes issues like memory leaks far less probable.

    In general, you have to do little to nothing to have the memory occupied by your objects reclaimed by the system. As soon as an object no longer has any references to it, e.g. it is assigned to a variable, then it is eligible for garbage collection. When the GC runs, it will return the memory occupied by the object to the pool of available memory if it can, but it may have to make several passes in order to do that. To speed up that process, you should call Dispose on any object that supports it when you no longer need it. Disposing will release any managed and unmanaged resources (this does NOT include the memory it occupies) held by the object. If you don't do that, the GC must finalise the object the next time it runs and its memory cannot be reclaimed until next time. If you are defining your own type that holds managed or unmanaged resource then you should have that class implement the IDisposable interface and call its Dispose method when done with an instance.

    If a class doesn't have a Dispose method then the only thing you have to do to help clean it up is to make sure that an unused instance isn't assigned to a variable anywhere for a long time. For example, if you create an object in the Load event handler of your startup form and then never use it again, don't leave it assigned to a member variable for the life of the application, ensuring that the memory it occupies is never used but can never be reclaimed. Local variables fall out of scope and thus usually don't need to be set to Nothing but, particularly with member variables, i.e. fields, it may be necessary to set a variable to Nothing so that the unused object it referred to can be garbage collected.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width