views:

56

answers:

1

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?
+2  A: 

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.

Jon Skeet
Exactly what I needed :-)
MadBoy