Can't override Dispose)( ?

  • Thread starter Thread starter Gawelek
  • Start date Start date
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
 
mistrzu a nie powinno byæ

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


pozdrawiam

Przemek Sulikowski
 
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.
 
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
 
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.
 
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().
 
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.
 
Back
Top