PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] question regarding dispose-VBForums
Results 1 to 7 of 7

Thread: [RESOLVED] question regarding dispose

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,625

    Resolved [RESOLVED] question regarding dispose

    i see in some place that after i display another form i need to dispose the variable
    is this necessary?
    if so is this the correct way?
    Code:
               Dim Frmc As New FrmCustomers
               Frmc.ShowDialog()
               Frmc.Dispose()
    regards
    salsa

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,383

    Re: question regarding dispose

    You don't (and can't) dispose a variable. It's the object that you need to dispose. If you display a form by calling Show then closing it will dispose it but if you display it by calling ShowDialog then it is up to you to dispose it. As with any short-lived, disposal objects, the proper way to do this is with a Using block, e.g.
    vb.net Code:
    1. Using dialogue As New SomeForm
    2.     If dialogue.ShowDialog() = DialogResult.OK Then
    3.         '...
    4.     End If
    5. End Using
    The object created with the Using statement is implicitly disposed when execution leaves the block, even if an unhandled exception is thrown.

    By the way, in relation to your other thread, inside that If block is where you would get the data from the dialogue, update the data source of the grid on the current form and then save the changes to the database.
    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

  3. #3

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,625

    Re: question regarding dispose

    can i use this?
    Code:
                     Using Frmc As New FrmCustomers
                                Frmc.ShowDialog()
                                 
                     End Using
    Last edited by salsa31; Nov 9th, 2018 at 09:16 AM.

  4. #4

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,625

    Re: question regarding dispose

    what will happen if i dont dispose it?

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,383

    Re: question regarding dispose

    Quote Originally Posted by salsa31 View Post
    can i use this?
    Code:
                     Using Frmc As New FrmCustomers
                                Frmc.ShowDialog()
                                 
                     End Using
    Yes you can, if there is no option for the user to cancel the dialogue.
    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

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,383

    Re: question regarding dispose

    Quote Originally Posted by salsa31 View Post
    what will happen if i dont dispose it?
    Then it will retain system resources until the garbage collector gets around to cleaning it up. You can leave it to the GC but that is bad practice. You should always dispose objects that you create when you're done with them if you possibly can. There are rare occasions when it's not possible but any time you only use an object within a single block, it's possible.
    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

  7. #7

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,625

    Re: question regarding dispose

    wonderfull
    appriciate it

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width