|
-
April 20th, 2008, 06:58 PM
#1
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
{
}
Error 1 cannot derive from sealed type 'System.Drawing.Bitmap'
-
April 20th, 2008, 07:30 PM
#2
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.
-
April 20th, 2008, 10:19 PM
#3
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.
-
April 21st, 2008, 01:54 AM
#4
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.
-
April 21st, 2008, 06:17 AM
#5
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
-
April 21st, 2008, 07:16 AM
#6
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.]
Last edited by opedog; April 21st, 2008 at 09:12 AM.
-
April 21st, 2008, 07:37 AM
#7
Re: What should I be inheriting from?
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|