It is a wonder how i went so long without the greatness of the vb.net Interfaces. We’ve all made classes that we about the same so we just made a base class to hold all of the like data and build from there. What about the times when the classes had absolutly nothing to do with each other but you need them to share common actions such as a run command or a poopsy command?
This is where the interface comes in. With an interface you can create your base actions or functions. then we can create our new class, code what we want to code with what ever names we want them to have then implement the connectors of the interface to those specific functions or events. here is some very basic code to get the idea. there are many ways to use or even abuse this so use with care
Module Module1
Sub Main()
Dim someclass As MyCoolInterface = InitInterface(MyClasses.Class1)
AddHandler someclass.CoolEvent, AddressOf cooleventhandler
someclass.run()
someclass.Poopsy()
someclass = InitInterface(MyClasses.Class2)
AddHandler someclass.CoolEvent, AddressOf cooleventhandler
someclass.run()
someclass.Poopsy()
End Sub
Function InitInterface(ByVal cname As MyClasses) As MyCoolInterface
If cname = MyClasses.Class1 Then
Return New Class2
ElseIf cname = MyClasses.Class2 Then
Return New Class2
Else
'somehthings wrong
Return Nothing
End If
End Function
Public Sub cooleventhandler()
'do something when event is triggered.
End Sub
End Module
Public Enum MyClasses
Class1
Class2
End Enum
Public Interface MyCoolInterface
Sub run()
Sub Poopsy()
Event CoolEvent()
End Interface
Public Class Class1
Implements MyCoolInterface
Public Event CoolEvent() Implements MyCoolInterface.CoolEvent
Public Sub Poopsy() Implements MyCoolInterface.Poopsy
'do some stuff
RaiseEvent CoolEvent()
End Sub
Public Sub run() Implements MyCoolInterface.run
'do some more stuff
RaiseEvent CoolEvent()
End Sub
End Class
Public Class Class2
Implements MyCoolInterface
Public Event CoolEvent() Implements MyCoolInterface.CoolEvent
Public Sub Poopsy() Implements MyCoolInterface.Poopsy
'Do something else
RaiseEvent CoolEvent()
End Sub
Public Sub run() Implements MyCoolInterface.run
'do some more
RaiseEvent CoolEvent()
End Sub
End Class