LSB Posted June 18, 2004 Posted June 18, 2004 I'm running into trouble with my inherited userControls. I understood the basic concept of inheritance but have the following problem: The userControl derived from my base control doesn't simply have extra methods and properties but also enhances existing methods of the base control. Now I don't want to override the method but add something to it. E.g. the base draws something in the paint event. The derived control should paint the same as the base but some additional stuff. Do I have to write the whole paint event again or just call base.paint ? I don't know when methods in the base class are called or wether they're called at all. When do I have to call methods in the base manually and when are they called automatically? Same question goes for the constructor - is the base constructor called before the derived object is created? thx 4 any enlightment Quote
*Experts* Nerseus Posted June 18, 2004 *Experts* Posted June 18, 2004 First, the base class's constructors are called first, then the ones for the derived classes (in the order of the hierarchy). For overloaded events, like Paint, make sure your classes are overriding the default Paint method and not just using an event handler. To "hook" the paint event you can do one of two things (both I suppose - but no point): 1. Override the base class's Paint event. 2. Implement an event handler If you hook the event, then the derived class would also hook the event. It's probably best to override the Paint event handler in your case. In your derived class, you override it again and call "base.Paint(sender, e);" to call the one in the base class. Your base class, which also overrides the Paint event handler, would have the exact same line (base.Paint...). In general, it would be better for your base class to override a method. Your derived class would also override and call base.Method(). Depending on the method and your needs, you might call the base method as the very first thing you do, or the very last. Sometimes you might not want to call it all if you decide you don't want any of the base class's functionality. -ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.