The next refactoring item in the menu is “Extract Interface”:
This applies to a class module, so in order for it to be active, we need to have a class module selected.
We would want to extract an interface in order to abstract an existing class to create a dumbed down testing structure for testing another procedure that uses the class, or in order to use polymorphism for similar objects that will all use the same structure but need specific code for each type of object.
I am not currently using any interfaces in our library, so the above screen shot is not really valid. But if I had a class module I had created for a specific purpose and wanted to create an interface for testing, which I’ve done in previous articles, this option makes it very easy.
A good example of this might be for the HAL_Outlook class module above. If I wanted to test sending an email in another module, I might create an interface using the HAL_Outlook class to make a “fake HAL_OutlookFake” class which would pretend that an email is being sent so that another module which uses it could be tested without the actual side effect of sending an actual email. So if I extract an interface of the HAL_Outlook class, let’s see what happens.
Here are the default options for the dialog:
And the resulting code is:
In Module IHAL_Outlook:
'@Exposed
'@Interface
Option Explicit
And in the original HAL_Outlook it added this line in the beginning declarations:
Implements IHAL_Outlook
And even though IHAL_Outlook doesn’t contain any functions or methods, I still see the following when selecting the left dropdown and right dropdown in the HAL_Outlook class module:
Hmmm… that does not look like what I wanted. I expected it was going to extract all of the public subs / functions and then they would be renamed to use the interface name in front of the module.
Maybe if I select all the code in the module when extracting the interface it will work as I anticipated. Let me try that.
That did exactly the same thing. Not very useful… I will play with this more later and see if we can get it to do something that would actually be useful.
Trackbacks/Pingbacks