- How do you perform an "Extract Superclass" on an existing UserControl in .NET?
I then posted this question on the MSDN Forums.
Honestly, I was hoping that there would be a tool for doing this, or at least a documented process. No such luck.
So, I went ahead and did my best to perform the refactoring, all the while documenting the procedure.
To summarize, here are the steps:
As promised, here's an oversimplified list of the things I did to accomplish this:
Created a new UserControl to use as the base (let's just call it "BaseUserControl"). Moved private member variables, event handlers, and other methods pertinent to the functionality I wanted up into BaseUserControl. Copied control(s) I needed from OriginalUserControl into the BaseUserControl using the visual designer (it's just easier that way, plus resources are managed correctly). Hooked up BaseUserControl events to the event handlers using the visual designer (again, it was just easier). Commented out duplicate code in OriginalUserControl (i.e. the methods, etc. that were moved up) Changed the inheritance of OriginalUserControl from UserControl to BaseUserControl. Build, fix, build......and it worked! Now I can build a NewUserControl based on BaseUserControl and it will have all the functionality that I was wanting in the container, but I can still add more controls, etc. specific to each case I need it.
I just wanted to mention again why I wanted to do this in the first place, since it may seem weird to go through all this:
- First, I created a pretty useful control for a project I was working on, but didn't think about reuse (oops!).
- Naturally, it became apparent that I would need another similar component after the initial proof-of-concept was done.
- After determining that the functionality of the container could be used in other controls, I needed a way to pull that functionality up so that I could use inheritance to create new user controls based on the container.
Actually it wasn't as bad as I thought, though it sure would be nice if this was an automated process, or at least if there was a documented step-by-step how to on the subject.
At any rate, I sincerely hope this helps other developers faced with a similar challenge!
No comments:
Post a Comment