I am working on developing an application that talks about the USB sensor family. I created a basic implementation that uses the Sensor class. The class contains events and methods that allow you to interact with the sensor (there is also a multi-threaded task processor, but I will go with a simple example).
My problem is that this simple proof of an example concept works fine, but now I need to expand the application to support the entire sensor family. To do this, I created the BaseSensor class with all the appropriate methods and events, and then created several subclasses such as SensorA, SensorB and SensorC, which are all inherent in BaseSensor.
This seemed like a good use of polymorphism, so I created a Shared function in BaseSensor called Initialize that performs the initial USB connection and returns the correct object depending on the type of sensor (SensorA, SensorB, SensorC). This works great, but it seems that I cannot find a way to correctly declare a With Events object. See Sample Code for my delicacy.
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
Something is missing for me. How do I proceed?
Andrew Robinson
source share