0

I'm creating XML file which will hold couple of values regarding TCP Connection (IP (string), PORT (int), RetryCount (int), StartAutomatically (bool) and so on. There will be a number of TcpConnections defined like that (unknown to me).

What i thought is to create some kind of object called TcpConnectionHolder which i could create dynamically (one per tcp connection) that would hold all related fields so i could easily load all tcp connections from xml to that dynamic object and that i could later on reuse those fields, or update them from code when necessary.

My questions are:

  1. How do I create such object with multiple fields (example with more then one Value -> Data would be nice)
  2. How do I assign multiple values to one connection - (preferably both setting all values at once and one by one would be nice).
  3. How do I read it?

1 Answer 1

2

It looks like you just need one class (TcpConnection) with properties for the IP address, port, retry count etc.

I'd suggest some kind of structure like this:

public sealed class TcpConnection
{
    private readonly int port;
    public int Port { get { return port; } }

    // Or use one of the types from System.Net
    private readonly string ipAddress;
    public string IpAddress { get { return ipAddress; } }

    private readonly int retryCount;
    public int RetryCount { get { return retryCount; } }

    // etc

    public TcpConnection(XElement element)
    {
        // Extract the fields here
    }
}

(Alternatively, have a static factory method to extract the values from an XElement, and a constructor just taking the "raw" values.)

Then to store multiple values, just use a List<TcpConnection>.

This is neater than a single object storing multiple IP addresses, multiple retry counts etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.