KnowDotNet Visual Organizer

NET Refactor - Create Interface

Create an Interface for multiple classes.

Create Interface is a feature of NET Refactor that will automaticaly create an Interface for multiple classes in VB.NET or C#.  You use Interfaces when you have two or more classes that expose the same public members, but accomplish different purposes.  It allows you to create a common calling sequence for multiple classes and maintain only one set of code to call the classes, based on the object instantiated to Implement the Interface.

To create an interface for a class, place the cursor in the class that you want to Implement.  Click on the Create Interface menu option of NET Refactors Organizer menu.  The Interface will be automatically created and added to your project.  Additionally, the class will be updated to Implement the new Interface.

The following code shows a class that we want to build an Interface for.

Public Class Class2
  
Private _var As String
   Public Property Var() As String
      Get
         Return _var
      
End Get
      Set(ByVal Value As String)
         _var = Value
      
End Set
   End Property

   Public Sub MyPublicMethod(ByVal s As String)
      
' this sub exposes the entry method of the class

   End Sub

   Public Function MyPublicFunction(ByVal i As Integer) As String
      ' this function exposes another public member
   End Function
End
Class

Once the Create Interface menu option is clicked, the dialog in Figure 1 will be displayed.  You are presented with all of the public interfaces of the class.  If you do not want one or more of the public members to be included in the Interface, uncheck those items.

Figure 1 - Create Interface Dialog.

Create Interface Dialog



After creating an Interface, the code will look as follows:

Public Class Class2
  
Implements IClass2

  
Private _var As String
   Public Property Var() As String Implements IClass2.Var
      
Get
         Return _var
      
End Get
      Set(ByVal Value As String)
         _var = Value
      
End Set
   End Property

   Public Sub MyPublicMethod(ByVal s As String) _
      
Implements IClass2.MyPublicMethod
      
' this sub exposes the entry method of the class

   End Sub

   Public Function MyPublicFunction(ByVal i As Integer) _
      
As String Implements IClass2.MyPublicFunction
      
' this function exposes another public member
   End Function
End
Class


The Interface will be added to the project.  Its code is shown below.

Imports System
  
Public Interface IClass2
      
Sub MyPublicMethod(ByVal s As String)

      
Function MyPublicFunction(ByVal i As Integer) As String

      Property Var() As String

   End Interface

Try NET Refactor Free for 30 days or purchase now by clicking Download or Purchase.

Top of Page

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing