Making new TCPsocket global?

I am experimenting sending TCP messages.

Running the SynchronousTCPSocketExample and adding to the code (in the connect button)
socket.write(“Sending from Xojo ;”)
socket.flush
before closing the socket, the message is received in the other end. So far so good…

But say I would like to use this connection somewhere else in my program (outside the connect button) and keep the connection open, then I would need to make
Dim socket As TCPSocket
socket = New TCPSocket
global, so that I can just keep doing socket.write socket.flush socket.write socket.flush to send new values

I tried making socket (Type TCPsocket) in the properties and that worked.
But what do I do with
socket = New TCPSocket
I need to move that out of the connect button as well.

I admit I have a lot to learn about Xojo, so please…

Add a class (we’ll name it “classTcp”), change the Super to “tcpSocket” (without the quotes). Add a module. Add a property to that module (we’ll name it “myTcpSocket”) and Type to “classTcp”. In your application, either under Run event or Constructor method, type:

myTcpSocket = new classTcp

There you go. Reference everything as myTcpSocket… such as:

myTcpSocket.write("sending from xojo")

OK. I figured something out.
I added an external property, named it socketX, set the type to TCPsocket
In the (connect button) code I added: socketX=socket

Not exactly sure how works under the hood, but it does. In another button I can now just do socket.write socket.flush

Thank you! I got this answer while writing my last message. I will try that one later.

Is that the right way to do it or is the way I described it just as correct? (or not)

[quote=133207:@Roger Jönsson]Thank you! I got this answer while writing my last message. I will try that one later.

Is that the right way to do it or is the way I described it just as correct? (or not)[/quote]

Sure, you can do it that way!