kejpa Posted June 18, 2004 Posted June 18, 2004 Hi, I'm having a object hierarchy where I want to raise events in the derived classes but it's not working the way I want it to. Public Interface iSensor Event NewData(ByVal SensorValues() As Integer, ByVal TimeStamp As Single, ByVal Sensor As iSensor) . Sub ReadSensor() . End Interface Public MustInherit Class c485Sensor Implements iSensor Public Event NewData(ByVal SensorValues() As Integer, ByVal TimeStamp As Single, ByVal Sensor As iSensor) Implements iSensor.NewData Public MustOverride Sub ReadSensor() Implements iSensor.ReadSensor . End Class Public Class c485Specialized Inherits c485Sensor Public Shadows Event NewData(ByVal SensorValues() As Integer, ByVal TimeStamp As Single, ByVal Sensor As iSensor) . Public Overrides Sub ReadSensor() RaiseEvent(imodValues, DateAndTime.Timer,Me End Sub If I shadows the event in the 485Specialized class it won't be raised unless I explicitly calls it from the Applications Form : AddHandler ctype(oSensor(0),c485Specialized).NewData, AddressOf oSensor_SensorData What I want is to raise the event in the derived class without having to ctype it. Is there something wrong with my hierarchy? Shouldn't the "Shadows" keyword shadow the base class procedures and let the derived classes handle the calls? I'm confused. Again :confused: Thank God it's Friday /Kejpa Quote
*Experts* Nerseus Posted June 18, 2004 *Experts* Posted June 18, 2004 To "call an event in a derived" class, you need only call a function in your base class and mark the base method as overridable (VB) or virtual (C#). For example: Base class A: Create a method "CallMe" with no code (unless you want seme default behavior, in which case put in some code). Make sure it's marked overridable or virtual. From the method that you want to raise the event from, just call this method (CallMe). Inherited Class B (inherts from A): Override the method CallMe to do whatever you want. When class A needs to "call the event" it's really just calling the method in class B. You would only need events if you want an outside class to get the event. In that case, do the above AND have class A's CallMe raise an event that an outside class could hook into. (If you do this, then class B's CallMe method would first call base.CallMe() to get that default behavior of raising the event.) -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.