Class or Structure?

Which is the better way to handle data input/output messages from a TCPSocket? Using a Class or a Structure? Opinions? Thanks!

my opinion… class should almost always be used in reference to a structure… unless a structure is REQUIRED (such as for declares), OR the need is simple (such as storing a POINT coordinate value)

@Dave S So a structure inside a class instead of just a property in the class?

If the data you receive is “structured” (ie one field after another) then a structure may be useful to help interpret the data
But in the rest of your code a class is almost always to be preferred except like Dave notes

And keep in mind that a TCPSocket does NOT insure that you receive the entire thing in one “DataAvailable” event
While that can happen frequently its not guaranteed
This is probably the most frequent bad assumption made with sockets

@Norman Palardy What if the data is just one long string? Is a structure needed in that case?

A structure OR a class

I would use a Xojo.Core.Dictionary which can be easily serialised to and from json string using

Suppose you wer sending a “record” that consisted of Name, address, postal code etc
That might be sent from the client as a structure (if the fields are of fixed size)
And on the recieving end you’d accumulate things until you got an “end of record” signal of some kind then toss all the data into a structure to be able to get at the parts easily

If its just a string how do you know where it begins & ends ?
And whats IN the string ?

Its entirely possible that the sender sends “123456789” and your receiver gets it chunked up “123”, “456”, “789” in several data available events

I like the idea of using a class because each time I call new class that guarantees the data will never overlap if two data streams are processed simultaneously. Is that a correct assumption?

While dictionaries and arrays and other built in mechanisms are handy I’d not suggest them as alternatives for classes
You might have a dictionary of classes or an array of classes

Not sure what you mean ?
If you have two instance then their data will not be shared between then - thats correct
BUT if you are sending & receiving data across TCPSockets then its entirely possible to have ONE socket send data from any instance and then the other end has to have some way to separate it

I’m using a serversocket and I’m trying to make sure that the data that each TCPsocket is processing that is using the same methods will never overlap. I was thinking using a class would ensure this for me because currrently I am using a structure.

Thanks for all of the input I really appreciate it.

If the structure is a non-shared property of your TCPSocket subclass, then each socket has their own separate copy. Class vs. Structure doesn’t really make a difference. What makes a difference is Global variable vs. subclass variable. As others have said, a structure is useful for fixed-length fields in a block of data.

@Tim Hare Thanks for the response. I’m currently using a public class to hold the input/output data that is coming into my TCPSocket to process it. I didn’t want to use a global variable for the very reason you are describing. I need to be able access the data from other methods outside of the class and TCPSocket class. If I create a new class instance of the string variable in DataAvailable for each data stream coming into the TCPSocket will the TCPSocket always have a separate copy of the data that will never overlap?

each tcp socket instance connects to one “other end” whatever that is (since the tcp socket could be the client or server end)

its all coming from one other connected socket

@Tim Hare Ok So I got rid of the class and I created a private string variable in my TCPSocket class. I also moved the methods processing the data from my TCPSocket into the TCPSocket class and made them private. So now will the properties and methods that are in my TCPSocket class have their own separate copy to use for each socket created by the serversocket? Thanks!

as a side note… A class can have other classes as properties. A class can even have a property that is an instance of the same class type (I use this to create a clone method)

Yes, and the methods don’t have to be private for that to be true. Also note that the methods don’t have to be part of the socket for that to be true. They just can’t use any global variables. Every time you call a method, it creates a new copy of its local variables on the stack. So the method can be global, but its local variables are “instanced” every time you call it.

That said, moving things as close to the source as possible is always desirable. So by putting the methods in the class and making them private, you have “encapsulated” them. That is a good thing.

@Tim Hare Thanks so much as usual you helped me a lot!