Hello,
I'm working on developing an application that talks to a family of USB sensors. I've created a basic implementation that utilizes a class called Sensor. The class contains events and methods that allow for interaction with the sensor (there is also a threaded task processor involved but I'll go with a simple example).
My issue is that this simple proof of concept example works fine but now I need to expand the application to support the whole family of sensors. To do this I've created a BaseSensor class with all the appropriate methods and events and then I've created multiple subclasses such as SensorA, SensorB, and SensorC that all inherent BaseSensor.
This seemed like a good application of polymorphism so I've created a Shared function on BaseSensor called Initialize that does an initial USB communication and returns the correct object depending on the sensor type (SensorA, SensorB, SensorC). This works fine however it seems I can not find a way to correctly declare the object With Events. See the sample code for my deliema.
Attempt 1:
Public Class Form1
Dim WithEvents oBaseClass As BaseClass
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
oBaseClass = New ExtendedClass
oBaseClass.Test() 'This doesn't work because the object was type casted.
End Sub
Private Sub TestEventHdlr() Handles oBaseClass.TestEvent
MsgBox("Event Fired")
End Sub
End Class
Public Class BaseClass
Public Event TestEvent()
End Class
Public Class ExtendedClass
Inherits BaseClass
Public Sub Test()
MsgBox("Test")
End Sub
End Class
Attempt 2:
Public Class Form1
Dim WithEvents oBaseClass 'This doesn't work.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
oBaseClass = New ExtendedClass
oBaseClass.Test()
End Sub
Private Sub TestEventHdlr() Handles oBaseClass.TestEvent
MsgBox("Event Fired")
End Sub
End Class
Public Class BaseClass
Public Event TestEvent()
End Class
Public Class ExtendedClass
Inherits BaseClass
Public Sub Test()
MsgBox("Test")
End Sub
End Class
I'm missing something here. How should I proceed?