I extracted an interface in the last two articles for an encryption algorithm class. It’s pretty basic, but that’s ok, I’m just playing with the various refactoring tools to get familiar with them and understand how they work.
Now I want to implement an Interface. I’m going to Refactor the interface I created and rename it from “IHAL_RC4Cls” to “IHAL_Encryption”. That was simple. This is a more generic interface name so that I could create multiple classes based on this interface to encrypt and decrypt strings. The refactor changed the name of the interface class, and updated the Implements statement in the class that implements it already, and it also correctly changed all the function names in that class that implement the interface. So all is good there.
Now I will implement the interface into a new class. I right click on the interface in the VBE Project pane and tried to find a RubberDuck Implement interface option, but it’s not there.
Then I tried to go up to the Rubberduck Menu to choose the Refactor -> Implement Interface option, but it is grayed out.
I tried selecting the interface in the IDE, opening the interface in the editor, and placing my cursor in the interface, but still the option is grayed out.
I was expecting to use Implement Interface to create a new class for me that would use the implements keyword and setup the class with the needed functions. This is NOT how it works though.
But if I create a new class and add the Implements IHAL_Encryption keyword at the top like this:
Class 1
Option Compare Database
Option Explicit
Implements IHAL_Encryption
Then if I place my cursor on the Implements line and choose the RubberDuck menu the option to Implement Interface is still grayed out.
Now I will try saving the class and see what happens. Ok, now I can right click on the Implements IHAL_Encryption line and choose RubberDuck->Refactor->Implement Interface
There is no dialog, but it updates the code in my class to include the functions required by the interface like this:
HAL_BasicEncryption
Option Compare Database
Option Explicit
Implements IHAL_Encryption
Private Function IHAL_Encryption_encrypt(sInput As String, sKey As String) As String
Err.Raise 5 'TODO implement interface member
End Function
Private Function IHAL_Encryption_decrypt(sEncryption As String, sKey As String) As String
Err.Raise 5 'TODO implement interface member
End Function
So there it is. It correctly added the functions that the interface requires. It also adds the TODO comment and sets up the new functions to raise error 5. Which displays the following when I raise it:
Seems like a potentially sensible thing to do. It will direct the programmer to add code to the procedures so they won’t error out. If you’re doing TDD this will work too because it will automatically ensure that tests using the new procedures will fail.
This feature helps save time with typing out the functions or choosing the class and function dropdowns to implement all the public elements. Seems like a nice feature.