I don't know what your DoSomething function does, but I'm trying to assign an instance of CssClass there for testing purposes.
Declare the interface as follows:
Public Interface IMyInterface(Of Out T As WebControl) Function DoSomething() As T End Interface
Note the Out T parameter.
Create two controls that implement the interface:
Public Class MyCustomControl1 Inherits CompositeControl Implements IMyInterface(Of MyCustomControl1) Public Function DoSomething() As MyCustomControl1 Implements IMyInterface(Of MyCustomControl1).DoSomething ' do stuff Me.CssClass = "XXX" Return Me End Function End Class Public Class MyCustomControl2 Inherits CompositeControl Implements IMyInterface(Of MyCustomControl2) Public Function DoSomething() As MyCustomControl2 Implements IMyInterface(Of MyCustomControl2).DoSomething ' do stuff Me.CssClass = "YYY" Return Me End Function End Class
On the test page PageLoad:
Dim someCustomControl As New MyCustomControl1 Dim someCustomControl2 As New MyCustomControl2 Dim myList = New List(Of IMyInterface(Of WebControl)) myList.Add(someCustomControl) myList.Add(someCustomControl2) myList.ForEach(Sub(i) Literal1.Text &= i.DoSomething.CssClass & "<br />")
As a result, the CssClass property of both someCustomControl and someCustomControl2 is set to the appropriate values.
This indicates that the DoSomething interface function was successfully called and the instance has changed.
source share