What should I be inheriting from?
I am trying to do something I'm not supposed to do as usual...
I wanted to extend the Bitmap class, but it is 'sealed'.
What am I supposed to be inheriting from, if not the Bitmap class?
Code:
class MyBitmap:Bitmap
{
}
Quote:
Error 1 cannot derive from sealed type 'System.Drawing.Bitmap'
Re: What should I be inheriting from?
If a class is marked sealed, you can't inherit from it (that's what the sealed keyword is for).
What are you trying to do? Perhap there is another approach you can take other than using inheritance.
Re: What should I be inheriting from?
I have a bitmap and I calculate the mean and std dev on it.
I'd like to attach that to the class so that I can pass the class around and not have to recalculate them.
Re: What should I be inheriting from?
Just use composition instead of inheritance.
In other words, create a class that contains a Bitmap field and additional methods and fields to track the mean and std dev.
Re: What should I be inheriting from?
Excellent suggestion by Arjay. I would add that if you need the users of the class to have access to the bitmap you can give them read only access through a property
Code:
private Bitmap bitmap = new Bitmap();
public Bitmap Bitmap
{
get { return bitmap; }
}
If you want the users of the class to think that the are working with a bitmap you would have to provide wrapper methods for the Bitmap methods that the users would be expecting. I don't think this is the best option... but I thought I would mention it for completeness :)
Re: What should I be inheriting from?
Your other options include inheriting from System.Drawing.Image. Bitmap actually inherits from Image, anyway.
Or you could use what's called extension methods.
http://weblogs.asp.net/scottgu/archi...n-methods.aspx
Of course, if you're not using .NET 3.5 then extension methods won't be available to you.
But they sure are fun. :)
[Edit for nelo's wisdom.]
Re: What should I be inheriting from?
Quote:
Of course, if you're not using .NET 3.0 then extension methods won't be available to you.
But they sure are fun.
I think you'll find that extension methods are a feature of C#3.0 which is supported in .NET 3.5 to my knowledge. I do agree that they are fun :)