13 Replies - 933 Views - Last Post: 16 February 2012 - 03:19 PM Rate Topic: -----

Topic Sponsor:

#1 vmagdum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-February 12

Dispose method in Using Statement.

Posted 07 February 2012 - 11:08 PM

I have Overloaded the Dispose method of System.Windows.Forms.Form Class in which I have written some extra code which I need to execute when object of Form Disposes. But when I create the object of Form in 'Using' statement, the 'End Using, statement do not execute my overloaded Dispose method. What should I do?
Is This A Good Question/Topic? 0
  • +

Replies To: Dispose method in Using Statement.

#2 trevster344  Icon User is offline

  • Slick.
  • member icon

Reputation: 124
  • View blog
  • Posts: 873
  • Joined: 16-March 11

Re: Dispose method in Using Statement.

Posted 08 February 2012 - 06:52 AM

If I'm not mistaken, using statement automatically disposes of any objects it has before the end, that is it calls the dispose method of that particular object, so you might try a with statement instead. That is the only reason I can see that you would have any issue getting around to your overloaded dispose. Why are you using the forms dispose method? Also might I see your code?
Was This Post Helpful? 1
  • +
  • -

#3 vmagdum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-February 12

Re: Dispose method in Using Statement.

Posted 11 February 2012 - 05:34 AM

I am sending my code as an attachment. When I create a object of MyForm and call its Dispose method, the overloaded Dispose method gets called. But while using 'Using' statement to create the object of MyForm, the 'End Using' statement do not calls the overloaded Dispose method.
Was This Post Helpful? 0
  • +
  • -

#4 nK0de  Icon User is offline

  • can't spell BITCH without IT
  • member icon

Reputation: 183
  • View blog
  • Posts: 732
  • Joined: 21-December 11

Re: Dispose method in Using Statement.

Posted 11 February 2012 - 06:03 AM

I see no attachment. It's best if you post your code here.
Was This Post Helpful? 0
  • +
  • -

#5 trevster344  Icon User is offline

  • Slick.
  • member icon

Reputation: 124
  • View blog
  • Posts: 873
  • Joined: 16-March 11

Re: Dispose method in Using Statement.

Posted 11 February 2012 - 09:02 AM

Read my post.
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 757
  • View blog
  • Posts: 2,822
  • Joined: 02-July 08

Re: Dispose method in Using Statement.

Posted 11 February 2012 - 10:14 AM

What's wrong with the Form Closing event. You can dispose any object there. It might be a good idea to show us what your talking about - there might be a best practice thing we can advise on.
Was This Post Helpful? 0
  • +
  • -

#7 vmagdum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-February 12

Re: Dispose method in Using Statement.

Posted 12 February 2012 - 06:35 AM

Sorry, I didn't attach the code last time. Please see the code I have attached and reply.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#8 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Dispose method in Using Statement.

Posted 12 February 2012 - 06:40 AM

Please post your code in code tags like:
:code:
We are not interested in downloading attachment for security reasons.....
Was This Post Helpful? 0
  • +
  • -

#9 vmagdum  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-February 12

Re: Dispose method in Using Statement.

Posted 15 February 2012 - 06:51 AM

Below is my code:-

Public Class MyForm
    Inherits Form

    Public Overloads Sub Dispose()
        MyBase.Dispose()

        MsgBox("Disposed")
    End Sub
End Class



Dim form1 As New MyForm

form1.Dispose() 'This calls the overloaded Dispose Method and MsgBox get displayed.

Using myForm As New MyForm
      myForm.Text = "CHILD_FORM"
      myForm.ShowDialog()
End Using 'But this statement do not calls the overloaded Dispose Method and MsgBox do not get displayed.


Was This Post Helpful? 0
  • +
  • -

#10 trevster344  Icon User is offline

  • Slick.
  • member icon

Reputation: 124
  • View blog
  • Posts: 873
  • Joined: 16-March 11

Re: Dispose method in Using Statement.

Posted 15 February 2012 - 10:25 AM

I'm not sure using statements call overloaded methods.
Was This Post Helpful? 0
  • +
  • -

#11 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 162
  • View blog
  • Posts: 563
  • Joined: 03-February 10

Re: Dispose method in Using Statement.

Posted 16 February 2012 - 11:31 AM

Using statements do not call overloaded Dispose methods.
Was This Post Helpful? 0
  • +
  • -

#12 trevster344  Icon User is offline

  • Slick.
  • member icon

Reputation: 124
  • View blog
  • Posts: 873
  • Joined: 16-March 11

Re: Dispose method in Using Statement.

Posted 16 February 2012 - 12:51 PM

Exactly why I was saying don't use a using method, especially if you're trying to call the dispose method, just call it. The using statement is for a little help managing your memory.
Was This Post Helpful? 0
  • +
  • -

#13 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 55
  • View blog
  • Posts: 250
  • Joined: 12-January 11

Re: Dispose method in Using Statement.

Posted 16 February 2012 - 01:43 PM

Just for the sake of learning (since I am not much of an extension writer yet):

In order to be able to use the Using statement, would you be able to write some kind of code to alter IDisposable since that is what the Using statement refers to?
Was This Post Helpful? 0
  • +
  • -

#14 trevster344  Icon User is offline

  • Slick.
  • member icon

Reputation: 124
  • View blog
  • Posts: 873
  • Joined: 16-March 11

Re: Dispose method in Using Statement.

Posted 16 February 2012 - 03:19 PM

You won't be able to use the using statement because you can't edit the disposable interface only inherit it, that being said you'll only be able to create your own class to inherit the interface and the dispose method and the using statement won't call that one. I could be wrong.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1