Answered by:
method overriding, calling base methods

Question
-
In method overriding, where will i call the base methods, before of after the code?
Ex.
public override void Refresh() { <code here> base.Refresh(); } ============OR================ public override void Refresh() { base.Refresh(); <code here> }
Always remember to say "Thanks" and do necessary actions to show your appreciation. Thanks!Thursday, December 15, 2011 3:00 AM
Answers
-
Depends on what you want to do.
If you for instance override the OnPaint Method of a Control, you will most likely draw some extra stuff after the base.OnPaint, or you might want to remove the baseclass call completely (often in a scenario where for instance OnPaintBackround is overridden and you simply want the program to do nothing at all at this event)
...
Regards,
Thorsten
- Proposed as answer by JMCF125 Thursday, December 15, 2011 2:58 PM
- Marked as answer by Bob ShenModerator Monday, December 26, 2011 4:48 AM
Thursday, December 15, 2011 4:22 AM -
Sometimes you will want to do the first, sometimes the second, sometimes you won't include the base call at all, sometimes you will have custom code both before and after the base call, and other times you may conditionally make the base method call.
It all depends on the specifics of the method you are overriding; how the base method works, and what you are attempting to do when you override it.
- Proposed as answer by JMCF125 Thursday, December 15, 2011 2:58 PM
- Marked as answer by Bob ShenModerator Monday, December 26, 2011 4:49 AM
Thursday, December 15, 2011 2:55 PM -
Hi,
when overriding WndProc, you usually listen to special messages, so you would use a switch for that. And also usually you want to do something when that special message is processed. So most likely you would first place your code, and when that message comes do your stuff and maybe return out of the method with a result set to that message (like 1) and ignore the base-call (for this message) or call the base still after your processing.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.wndproc.aspx#Y351
Example:
Regards,
Thorsten
- Edited by Thorsten Gudera Friday, December 16, 2011 3:10 AM
- Marked as answer by Bob ShenModerator Monday, December 26, 2011 4:49 AM
- Unmarked as answer by Arvin Granados Thursday, February 9, 2012 2:44 AM
- Marked as answer by Arvin Granados Thursday, February 9, 2012 2:44 AM
Friday, December 16, 2011 2:57 AM -
"How about overwriting WndProc"
Basically the answer is the same as my last answer. You can do so many things when overriding a method like that. There will be reasons for having your code first, reasons for having your code last, and reasons for always, or conditionally, not calling the base method.
Oh, and there's one more that I forgot. There will be times where it really doesn't matter whether it's before or after the base call. In reality, this makes up a significant portion of overridden methods.
- Marked as answer by Bob ShenModerator Monday, December 26, 2011 4:49 AM
- Unmarked as answer by Arvin Granados Thursday, February 9, 2012 2:44 AM
- Marked as answer by Arvin Granados Thursday, February 9, 2012 2:44 AM
Friday, December 16, 2011 2:14 PM
All replies
-
Depends on what you want to do.
If you for instance override the OnPaint Method of a Control, you will most likely draw some extra stuff after the base.OnPaint, or you might want to remove the baseclass call completely (often in a scenario where for instance OnPaintBackround is overridden and you simply want the program to do nothing at all at this event)
...
Regards,
Thorsten
- Proposed as answer by JMCF125 Thursday, December 15, 2011 2:58 PM
- Marked as answer by Bob ShenModerator Monday, December 26, 2011 4:48 AM
Thursday, December 15, 2011 4:22 AM -
Hi,
According to your logic you can wirte u r code.
public override void Refresh() { base.Refresh(); <code here> }
PS.Shakeer HussainThursday, December 15, 2011 4:24 AM -
Sometimes you will want to do the first, sometimes the second, sometimes you won't include the base call at all, sometimes you will have custom code both before and after the base call, and other times you may conditionally make the base method call.
It all depends on the specifics of the method you are overriding; how the base method works, and what you are attempting to do when you override it.
- Proposed as answer by JMCF125 Thursday, December 15, 2011 2:58 PM
- Marked as answer by Bob ShenModerator Monday, December 26, 2011 4:49 AM
Thursday, December 15, 2011 2:55 PM -
Sometimes you will want to do the first, sometimes the second, sometimes you won't include the base call at all, sometimes you will have custom code both before and after the base call, and other times you may conditionally make the base method call.
It all depends on the specifics of the method you are overriding; how the base method works, and what you are attempting to do when you override it.
How about overwriting WndProcis it safer to call base.WndProc(ref m); before my code?
Always remember to say "Thanks" and do necessary actions to show your appreciation. Thanks!Friday, December 16, 2011 2:44 AM -
Hi,
when overriding WndProc, you usually listen to special messages, so you would use a switch for that. And also usually you want to do something when that special message is processed. So most likely you would first place your code, and when that message comes do your stuff and maybe return out of the method with a result set to that message (like 1) and ignore the base-call (for this message) or call the base still after your processing.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.wndproc.aspx#Y351
Example:
Regards,
Thorsten
- Edited by Thorsten Gudera Friday, December 16, 2011 3:10 AM
- Marked as answer by Bob ShenModerator Monday, December 26, 2011 4:49 AM
- Unmarked as answer by Arvin Granados Thursday, February 9, 2012 2:44 AM
- Marked as answer by Arvin Granados Thursday, February 9, 2012 2:44 AM
Friday, December 16, 2011 2:57 AM -
"How about overwriting WndProc"
Basically the answer is the same as my last answer. You can do so many things when overriding a method like that. There will be reasons for having your code first, reasons for having your code last, and reasons for always, or conditionally, not calling the base method.
Oh, and there's one more that I forgot. There will be times where it really doesn't matter whether it's before or after the base call. In reality, this makes up a significant portion of overridden methods.
- Marked as answer by Bob ShenModerator Monday, December 26, 2011 4:49 AM
- Unmarked as answer by Arvin Granados Thursday, February 9, 2012 2:44 AM
- Marked as answer by Arvin Granados Thursday, February 9, 2012 2:44 AM
Friday, December 16, 2011 2:14 PM