Interface Delegation

Interface Delegation can be used to, well, delegate implementation of an interface and all it's members to a local field or property of the class (or structure).

Essentially, the class can declare itself to implement an interface, without actually providing an implementation for the members of this interface, itself. Instead, it can mark one of its properties or fields as providing the implementation for it. The member must, at runtime, contain a type that does implement the interface in question.

Public Interface Foo
  Sub DoFoo()
End Interface

...

Public Class Bar
  Implements IFoo
  
  ' Implementation of IFoo is delegated to an instance of FooHelper
  Private Property Helper As FooHelper Implements IFoo = New FooHelper()
  
End Class

...

Class FooHelper
  Implements IFoo

  Sub DoFoo()
    ..
  End Sub

End Class

Of course, Implements is also supported on "full" properties with custom Get/Set implementations:

Public Class Bar
  Implements IFoo
  
  ' Implementation of IFoo is delegated to an instance of FooHelper
  Public Property Helper As FooHelper 
    Implements IFoo
    
    Get
      ...
    End Get
    
    Set
      ...
    End Set
  End Property
  
End Class

Implements can be used on plain fields, as well:

Public Dim Helper As FooHelper Implements IFoo = New FooHelper()

By default, members from a delegated interface are not available on the type itself, but only by casting to the interface. Optionally, a Public or Private (the default) visibility modifier can be provided to make the interface members available on the class, as well:

Private Property FHelper As FooHelper Implements Public IFoo = New FooHelper()
Private Property BHelper As BarHelper Implements Private IBar = New BarHelper()

See Also