event in subclassing

Hi:
If I would like to add custom event to SerialPort by subclassing, how do I do it?

I added a event definition like say ProcessBuffer( inputString as string)

At Window 1 , I am able to add Event Handler for ProcessBuffer and Dataavailable. I tried to place RaiseEvent ProcessString(mBuffer)

I got an error “The item does not exist”

What am I doing wrong?

did u create event definition for ProcessBuffer and Dataavailable??

[quote=145563:@simon ong]Hi:
If I would like to add custom event to SerialPort by subclassing, how do I do it?

I added a event definition like say ProcessBuffer( inputString as string)

At Window 1 , I am able to add Event Handler for ProcessBuffer and Dataavailable. I tried to place RaiseEvent ProcessString(mBuffer)

I got an error “The item does not exist”

What am I doing wrong?[/quote]

You have to do all of the raising events inside the subclass. You can’t raise an event from the window where the SerialPort is placed.

That’s what is going wrong. If you want to force an event to raise from outside the subclass, create a method in the subclass that then raises the event. Then you can effectively force a raising of the event from the window.

Make sense?

More importantly: what would be the point? Why not just use the method?

Note: serious question. I always wonder when it is better to use one or the other.

[quote=145629:@Markus Winter]More importantly: what would be the point? Why not just use the method?

Note: serious question. I always wonder when it is better to use one or the other.[/quote]

Because, an event allows you to do something with the subclass that is relative to the window where you are working. So let’s say I have an event called BaudRateChanged for a serial port.

I can then raise that and update labels in my window from the raised event. Then anywhere I put that subclass, I can do similar things. Otherwise, if I just had the method, I’d have to make explicit calls to the labels on the window or I’d have to pass in the window object, etc.

Plus you could have an event that is triggered from some code or action inside the subclass. But there may be times when you still want to trigger that event externally.

Events allow you to get outside the object which is where their power comes into play…