Results 1 to 19 of 19

Thread: Class "Terminate Event"

  1. #1

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632

    Class "Terminate Event"

    OK...I am going to use VB6 code here...I know, I know...but I don't know .NET off the top of my head just yet.
    I have a class clsWoof
    VB Code:
    1. Public Class Class1
    2.     Protected Overrides Sub Finalize()
    3.         MsgBox("Class Terminated")
    4.         MyBase.Finalize()
    5.     End Sub
    6. End Class
    Ok, now I am assuming that Finalize in .NET is the same as Terminate in VB 6...am I correct in this assumption?

    Now in my form I have:
    VB Code:
    1. Private Sub cmdWoof_Click() 'VB 6 code, but u get the idea
    2. Dim objNew   As clsWoof
    3.    objNew = New clsWoof
    4.    objNew = Nothing
    5. End Sub
    OK, now I would expect the msgbox to appear when I click the button...NOT when I close my app down...I don't understand? Am I missing something really simple???

    Woka (Just started .NET and is being really thick )

  2. #2
    Lively Member
    Join Date
    Jul 2003
    Location
    Kuala Lumpur (Malaysia)
    Posts
    92

    Hmmm...

    Hi,

    You are disturbing the Base Class Event. It will fire when the form event trigger.

    To solve this, put it into a Sub...

  3. #3
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    You have to bear in mind that in the brave new world of .NET, the lifetime of a class is ended when the garbage collector comes along and scoops it up some time after your program has released all references to it. In a small program this will not occur until you shut your app down.
    If you have code that you need to execute on Finalize - like releasing resources etc., then make your class implement the IDisposable interface and call the Dispose method to nuke it.
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  4. #4

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    WOT?!
    So that object in the above example wont get destroyed till my app terminates??? Surely that resuklts in a memory leak!...?

    So how would I implement this IDispose jobbie?

    Is this such a basic question that I shouldn't be asking it, like how do I change a caption of a label in vb6???

    Does anyone know of any good .NET resources for muppets like me?

    Woka

  5. #5
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    Don't worry about memory - it is managed for you - that's what managed code means.

    Good references for VB.net include the book:-
    Professional VB.Net by Wrox Press (ISBN 1-861004-97-4)
    and the websites:
    GotDotNet and DotNetForums (I am "Merrion" on both of those btw.)
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  6. #6
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    To implement IDisposable...

    VB Code:
    1. Public Class PrintJob
    2.     Implements IDisposable
    3.  
    4.  
    5.       Public Overridable Overloads Sub Dispose() Implements IDisposable.Dispose
    6.           '\\ Do your cleaning up here....
    7.     End Sub
    8. End Class
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You'd also then call the dispose method instead of setting it to nothing (or you can do both if really want to).
    VB Code:
    1. Private Sub cmdWoof_Click()
    2.    Dim objNew As New clsWoof
    3.    objNew.Dispose()
    4. End Sub

    Although if you don't have any resources to clean up then there is really no need to even call dispose or implement IDisposable.

  8. #8

  9. #9
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    .NET scares the **** out of me
    Going from VB6 to VB.Net is like going from a plastic spoon to a swiss army knife. Sure there's a lot of new things to learn but the amount of new stuff you can do totally makes up for it.
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  10. #10

  11. #11
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    New stuff that is really useful:
    Managed multithreading - like:

    VB Code:
    1. Dim ts As New System.Threading.ThreadStart(AddressOf woofProc)
    2.  
    3.   Dim thread As New System.Threading.Thread(ts)
    4.   thread.Start

    Or all the new graphics stuff in System.Drawing.

    Or having a vast number of new events for forms (like nearly all the ones that EventVB provided and some that it omitted)

    Or having direct access to the WndProc for any messages you create (no more subclassing as it was)

    Or serialization/deserialization of compound classes to-from XML.
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  12. #12

  13. #13

  14. #14
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Originally posted by Wokawidget
    Interesting...Hmmmmmmm.
    But you still can't have icons in menus Booooooooooo

    Woka
    It isn't realy that hard to make an ownerdrawn menu in .NET, so you can put in the menu whatever you like.
    Frans

  15. #15
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Don't forget about inheritance and reflection, two of my favs. Oh and delegates can come in handy once in a while.

  16. #16

  17. #17
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Originally posted by Wokawidget
    G'm-buh?

    Woka

    Is that in reference to the MDI Forms, the Icon Menus, or my post?

    Inheritance I'm sure you've probably heard about. Reflection is finding out information about your objects at runtime, like what the variable name is or what type of object it is. A few cool things about this is Enums can be transferred back and forth between their numeric value and the string name. It can actually do a ton more than that but that was the first thing I thought was cool. Delegates are like function pointers or a variable that holds a function instead of a value.

  18. #18

    Thread Starter
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    • G'mbuh?
    • m'buh?
    • m-b?
    • fl-m?
    • j g-m-fl'buh?

    Are all the same, they translate to "What? Please explain kind Sir."



    Are you trying to scare me away from .NET?! Grrrrrrrrrrrrrrrr

    Woka

  19. #19
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    There is nothing to fear except fear itself
    ...and some large animals
    ...and a women on her period
    ...and maybe a badger with young'ins
    ...or what about a few people we all know
    ...and sometimes clowns can be scary
    ...well anyway there may be things to fear but .NET isn't one of them.

    Its just like most things in the industry and sounds more complicted than it is.
    Last edited by Edneeis; Jan 16th, 2004 at 12:30 PM.

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