Scrollable control vs panel

Does it realy make a diff (performance,memory) when inherit from scrolablecontrol in stead of panel.

just curious

Remco


Answer this question

Scrollable control vs panel

  • Catalin Pop Sever

    There is no performance penalty for inheritance irrelevant of the depth of the hierarchy except when you are dealing with virtual members.  In this case there is an extra lookup that occurs to find the right method at runtime but again it is a single lookup (the method table is consolidated into a single lookup table when the type is loaded) and therefore is independent of the size of the hierarchy.  At least this is the way that I remember C# and .NET working.  C++ on the other hand I believe works by linking the method tables of base and derived classes together.  Where performance generally starts to get bad is when derived class methods call base class methods.  In this case the inheritance depth matters.  With a good optimizer however even this is probably going to be optimized away at runtime provided the base class method is simple.

    Size-wise the inheritance only adds the additional fields introduced in the derived class.  The methods are stored with the type definition independent of the instance so if your derived class has no additional fields then it'll be the same size as the base class.  The type definition will be larger but that is stored separately and shared by all instances so the overhead is neglible.

    In general it is a good idea to avoid deep hierarchies when the hierarchy doesn't really add any value.  However you shouldn't avoid hierarchies when they are appropriate just because of performance.  So if you want to use ScrollableControl or Panel as the base class your performance won't really change much.  You can use whichever meets your needs best.

    Michael Taylor - 10/14/05

  • S76

    Great Post Micheal

    Remco


  • Dietz

    Not really.  The main difference between a panel and ScrollableControl (of which a panel is one) is that panels are designed to group controls together while a scrollable control is more useful for dealing with a single logical control that needs to scroll (like an image that you want to be able to scroll).  Functionally Panel adds just a couple properties to handle controls and defers to ScrollableControl for everything else.

    Personally I'd use Panel as long as it didn't get in my way and then fall back to ScrollableControl if it did.

    Michael Taylor - 10/12/05

  • amitdk

    tnx   That's wat i thought
    but a scrollablecontrol can group controls together,  just like a panel
    the point is: I have a baseclass that' going to inherit a few times, and i know there is a penaulty from inheriting, and this way you remove a 'layer'

    Remco


  • Scrollable control vs panel