AddHandler and Serial

I am sill having fun with the Serial class. I have created a wrapper class (composition) rather than a subclass, so my wrapper has some useful things and a ‘SP’ property that is assigned a fresh instance of Serial when I want to open it. That part works well but I just want to explain the composition aspect in case it is relevant to the problem. (I am not using a Serial component dropped on a form)

In the code below I want to get the Serial DataAvailable event to fire my own MyDataAvailable event (created by adding an Event Definition to my wrapper class) but the code completion does not reveal the Serial DataAvailable event and so I get a nice red bug against the last line below. I have scratched my head and Googled docs + articles but I cannot work it out. Please somebody tell me what I am doing wrong

      SP = new Serial
      SP.SerialPort = System.SerialPort(portIndex)
      PortName = portName
      SP.Baud = BaudRateNameToValue(baudRate)
      SP.Bits = DataBitsNameToValue(dataBits)
      SP.Stop = StopBitsNameToValue(stopBits)
      
      AddHandler SP.DataAvailable AddressOf MyDataAvailable

Dumb question. Do you have MyDataAvailable defined in your class?

No it is a good question, I do but the problem seems to occur before that.

If I create a brand new app with just a window and then say, code the open event with

Dim s as new Serial()

AddHandler s. [tab] there is no DataAvailable event in the code completion.

If I forge ahead and type it out then I get a compile error ‘This item does not exist’. Changing Serial to TCPSocket() doesn’t show a DataAvailable event either? I feel like I am missing something important so I am blaming myself first of all but I cannot work it out.

  1. Events don’t autocomplete in this context.
  2. You can’t use an event in AddHandler. You cannot call events directly. Use a method as an intermediary and raise the event from the method.

[quote=11728:@Carl Clarke] Please somebody tell me what I am doing wrong

AddHandler SP.DataAvailable AddressOf MyDataAvailable [/quote]
There should be a comma between DataAvailable and AddressOf:

AddHandler SP.DataAvailable, AddressOf MyDataAvailable

Hi Tim
Hi Tim

  1. That is my observation.
  2. That is exactly what I am trying to do. I want to get the DataAvailable event from Serial to fire my own ‘intermediary’ event MyDataAvailable by using AddHandler.

Looking at the Language Reference for AddHandler there is an example for a Timer that works

  WindowTimer = New Timer
  
  WindowTimer.Period = 1000
  WindowTimer.Mode = Timer.ModeMultiple
  
  AddHandler WindowTimer.Action, AddressOf HandleEvent

My HandleEvent method gets invoked by the Timer every second and displays a MessageBox as expected. So why can’t I use AddHandler to do the same with the Serial DataAvailable event?

Hi Andrew

Sorry my fault - there is a comma in my source, I think I edited it out when I changed the name of the handler in this post.

Your method must have “something as serial” as the first parameter. Otherwise, the compiler won’t find a match.

Thanks Tim - that was it! I had it in then re-wrote it and forgot to put it back. Thank you very much of that, I am off to find some Paracetemol now.