This is another article on Refactoring, adding to the many that I have written on the subject of Refactoring VB.NET and C# code. This article is about Extract SuperClass. We would do this refactoring when we have two classes with similiar methods, properties, varaibles, etc., and we would like to extract common functionality into a SuperClass or parent class and have the original classe(s) inherit from the new class. If we do this manually, we would need to create a new class, which will be the SuperClass or Parent class, cut and paste the duplicated code from the original to the SuperClass and then have the original class(es) Inherit from the new Super Class. Depending on the size and complexity of the original class(es), this could be tedious.
KnowDotNet has just released this feature in the latest version of its most popular product, NetRefactor. The following figures will show this feature in action. Figure 1 shows a VB.NET class, from which we will create a SuperClass.
Figure 1 - Original VB.NET Class
The class shown above has three methods, a Public Property, and a Private variable for the property. We are going move the property, its private variable, and the class constructors to the SuperClass, leaving the three methods in what will become the child class. To select the original class, we only need to place the cursor anywhere in the class. NetRefactor will determine the start and end of the class. Next, we right-click in the class and select Extract SuperClass from the NetRefactor context menu. Figure 2 shows the Extract SuperClass dialog. It has extracted the definition line of all objects in the class and automatically checked all of them, assuming that you will want to extract many of the objects into the new SuperClass.
Figue 2 - Extract SuperClass Dialog.
Once the dialog is displayed, simply uncheck any objects that you do not want extracted, and they will be left in the original class. Having determined to leave the three methods, we uncheck them, which will cause the property, variable, and constructors to be extracted to the new class. Finally, we click the Ok button to extract the SuperClass. Figure 3 shows the new SuperClass at the top of the window, with what remains of the original class at the bottom of the window. Notice that the old class now inherits from the new SuperClass.
Figure 3 - New VB SuperClass.
So much for VB.NET, Figure 4 shows the same basic class written in C#, because all feaures of NetRefactor currently support both languages.
Figure 4 - Original C# Class.
We follow the same procedure outlined in the VB.NET section for C# classes. Figue 5 shows the Extract SuperClass Dialog for a C# class.
Figure 5 - Extract C# SuperClass Dialog.
Having selected the methods we want in the new SuperClass, we click the Ok button to create the new SuperClass shown in Figure 6. Please note that any comments preceding a method or other object will be picked up and moved to the new SuperClass along with the code.
Figue 6 - Extracted C# SuperClass.
It can't be any simpler to do this fairly complex refactoring.
Happy refactoring and give (download here) a test drive, only if you consider that your time is money, and the better producer you are, the more valuable and marketable you become!