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 = "127.0.0.1", AnotherProperty = "blah"});
Something like this
Hi there.
You could create a new class
specifically for sotring this information e.g.
public class Client
{
private string Name { get; set;}
private string IP { get; set;}
//... etc.
}
Instead of using a HashTable, simply store an instance of the Client
class into the Session
object (or whatever your using to store the HashTable.)
If you need to store more then one client's details, then use a List<Client>
, or a Dictionary if you need to use keys to lookup items. Experiment to see what works for you.
Cheers. Jas.
I'm not sure it's the best solution, but just for general knowledge, if you really need a table, you can use DataTable Class which is a part of ADO.NET (which is native to .NET).
The reason this is not advisable is that it doesn't support unique keys, as a dictionary, and that it generally complicates things in your scenario.