Can't override Dispose)( ?

G

Gawelek

When I write this code

namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
(...)
/// <summary>
/// Clean up any resources being used.
/// </summary>
public override void Dispose() <-------
{}
(...)
}

I obtain :

C:\Temp\CSharp\WindowsApplication1\Form1.cs(32):
'WindowsApplication1.Form1.Dispose()' : cannot override inherited member
'System.ComponentModel.Component.Dispose()' because it is not marked
virtual, abstract, or override

And It's written in msdn :
Component
[C#]
public virtual void Dispose();

What's wrong ?

Gawel
 
S

Selvin

mistrzu a nie powinno byæ

protected override void Dispose( bool disposing )
{
....
}


pozdrawiam

Przemek Sulikowski
 
N

Nicholas Paldino [.NET/C# MVP]

Gawelek,

You should be looking to override this method:

private virtual void Dispose(bool disposing)

The public Dispose method just calls this method with the disposing flag
set to true. The finalizer calls the above method with the disposing
parameter set to false. The disposing flag basically tells you whether or
not to call the Dispose method on any managed components that your instance
is holding that implement IDispose.

Hope this helps.
 
G

Gawelek

Gawelek,
You should be looking to override this method:

Why ?
private virtual void Dispose(bool disposing)

The public Dispose method just calls this method with the disposing flag
set to true. The finalizer calls the above method with the disposing
parameter set to false. The disposing flag basically tells you whether or
not to call the Dispose method on any managed components that your instance
is holding that implement IDispose.

Dispose() is marked virtual. Why can't I override it ?

Gawel
 
J

Jon Skeet

Gawelek said:
Dispose() is marked virtual. Why can't I override it ?

It's a bug in the documentation. Component.Dispose isn't actually
virtual - look at the Component class's IL and you'll see that. There
seem to be a number of methods which are documented as being virtual
but which aren't, in fact, virtual.
 
J

Jon Skeet

Mr.Tickle said:
You can implement IDisposable and make yer own , right?

Component already implements IDisposable though. The OP should be
overriding Dispose(bool) rather than just Dispose().
 
W

William B.

IDispose::Dispose is already implemented by the Component class. Since it is
not marked virtual (since it implements the interface), you can't override
it. This must be by design, forcing you to use the form dispose method
instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top