I've been struggling with this for hours. Hopefully some of you vb.net gurus can help restore my sanity.
Scenario : I have an object (mqtt_client) which exposes connect / disconnect events that I need to trap and deal with. I need the object to be accessible from multiple subs / functions / modules in my code. so I'm declaring it Public within the enclosing class but outside a code block.
If I declare it outside the main sub like this :
Public mqtt_client = New MqttFactory().CreateManagedMqttClient
Public Sub Ruptela_Server(sender As Object, e As EventArgs) Handles MyBase.Load
' Add Event Handlers for Connected and disconnected events
AddHandler mqtt_client.Disconnected, AddressOf MQTTclient_disconnected_handler
AddHandler mqtt_client.Connected, AddressOf MQTTclient_connected_handler
The addhandler fails as the events aren't exposed by mqtt_client and I'm not sure why.
However, If I do it this way :
Public Sub Ruptela_Server(sender As Object, e As EventArgs) Handles MyBase.Load
Dim mqtt_client = New MqttFactory().CreateManagedMqttClient
' Add Event Handlers for Connected and disconnected events
AddHandler mqtt_client.Disconnected, AddressOf MQTTclient_disconnected_handler
AddHandler mqtt_client.Connected, AddressOf MQTTclient_connected_handler
Then the addhandlers hook up fine, but then the object only has scope inside the sub and can't be access from a different module.
I can't put ALL of this code outside of the enclosing block as addhandler is a method and won't work there.
How should I be going about this? Any guidance would be gratefully received.