UDPSocket AddHandler for DataAvailable

Hi all,

I have created a class that will be used to discover devices on the LAN using UDPSockets. To keep the code somewhat organized. I have this class creating it’s own UDPsockets rather than placing them as controls on a window.

I’m not sure what I am missing but I define:

DiscoverSocket as UDPSocket

In constructor:

DiscoverSocket = new UDPSocket
DiscoverSocket.Port = 4850

Then I’m trying to add a handler for DataAvailable:

AddHandler DiscoverSocket.DataAvailable AddressOf DiscoverDataAvailable

However the IDE and compiler complain that there is no DataAvailable member on UDPSocket. But clearly there is from the help and if I stick on on a window.

I also tried subclassing and implementing DataAvailable (which is present when adding an event handler to the subclass). But I still cannot assign the handler.

I have done this successfully for TCPSocket and I’m wondering why it’s failing on UDPSocket. This is xojo 2021 3.1

Thanks in advance

Well I ended up subclassing UDPSocket for each discovery socket I needed and that seemed to work. In the sub classes I could get to the DataAvailable member.

Missing comma. (Credit to Norman for spotting it)

AddHandler DiscoverSocket.DataAvailable, AddressOf DiscoverDataAvailable

1 Like

Hi Andrew. The comma was not the issue. Cut/paste error. The problem still seem to be that trying to directly use a UDPSocket the DataAvailable member is just not there. Not sure why. As I said I got around this by creating my own subclasses and only then did the DataAvailable event handlers become available.

:person_shrugging:

Hi Julian,

Nope. I have exactly the code you show with the comma in place. … on DiscoverSocket yields NO DataAvailable in the list. Compile error says DiscoverSocket does not contain a member named DataAvailable.

Anyway fixed via sub classing.

Odd, you won’t see DataAvailable on the autocomplete for DiscoverSocket if that is what you’re referring to? Are you able to replicate that in a simple demo project?

The autocomplete will not work for AddHandler since it hasn’t done this if ever.

Right mouse click on “UDPSocket” (above code, in the ide) then click New Method → from event → DataAvailable. Now you get a method witth the correct signature and it should work.

The method should have the signature: UDPSocket_DataAvailable(Sender As UDPSocket)

Create a subclass of UDPSocket, call it USocket

Add a method to USocket, call it handleDataAvailable

USocket.handleDataAvailable(sender As USocket)
  // do stuff

Add a constructor to USocket

USocket.Constructor()
  Super.Constructor
  Port = 4850
  AddHandler DataAvailable, AddressOf handleDataAvailable

Add a button to your form or whatever

Button1.Action()
  Var USocket1 as USocket = New USocket
  // you probably want to assign USocket1 to a property to prevent it falling out of scope 

Tested on 2020R2.1 Windows

Hi all,

Thanks for the input. I did learn a few things like the right-click which I hadn’t thought of trying.

This was on 2021 R 3.1 on Mac M1Max and yes I did have the comma in place. For whatever reason when typing in the socket and there was no DataAvailable event in the list. So I typed it in. Compiled and got the error. And again, yes I had a comma in place between the event and the address of.

Like I do with most things after doing this for close to 40 years I find another way and the way I found which actually is nicer because I can encapsulate some of the specifics of each discovery socket was to sub class. Once I sub classed DataAvailable could be directly accessed and code put in to deal with the data.

This morning I restarted my Mac. With this M1Max and current OS I haven’t need to restart very often at all. I restarted this morning just because it had been close to 30 days. After restart, in my test program I was going to post here, suddenly DataAvailable is there in the list and it compiles.

No idea, no clue why a restart fixed it. But anyway thanks for all the attention to this thread. I feel that as I undertake this Xojo project I am on that this forum will be a great place to find answers.

Thanks again everyone.

Mark