CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2006
    Posts
    103

    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'

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  3. #3
    Join Date
    Nov 2006
    Posts
    103

    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.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  5. #5
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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

  6. #6
    Join Date
    Mar 2008
    Location
    Atlanta, GA
    Posts
    49

    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.

  7. #7
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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
  •  





Click Here to Expand Forum to Full Width

Featured