Alex Godbehere Alex Godbehere - 3 months ago 49
C# Question

Best way to store sets of data (In a table)

I have a client and a server program, and I am currently using hashtables to store the clients Name and IP address when they connect. I now need to add another variable that the client will send to the server when it connects, but as far as I understand it, hashtables only have 2 columns (Key and value). Is there another way I could store this data instead of using hashtables?


You can stick objects into hash tables, or dictionaries.

So make yourself a user class of some description and then store your user object in your dictionary under their name.

This would then lend itself to take other properties as and when you need to add them.

public class User
    public string Name { get; set; }
    public string IPAddress { get; set; }
    public string AnotherProperty { get; set;}

Dictionary<string, User> userTable = new Dictionary<string, User>();

userTable.Add(userName, new User(){Name = "fred", IPAddress = "", AnotherProperty = "blah"});

Something like this