Click here to Skip to main content
15,906,335 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a hashtable which is containing my tcpUsers, and i want to output this to a string list as an easy way to manage currently connect users. How would I do this. Any other easier method?

What I have tried:

I'm not sure where to start, but i want to just add a value onto the list, maybe into the next available index?
Posted
Updated 22-Dec-16 9:15am
v2
Comments
Philippe Mori 20-Dec-16 21:00pm    
If you have a hash table, it was probably done for performance purpose of supporting a lot of users with minimal overhead. So it probably does not make much sense to use a list instead. You probably cannot simply add user at the end since you probably have to ensure that the user is not already in the table. And you don't need a list to iterate over existing values. Also, where is your code?
Ramza360 22-Dec-16 14:46pm    
If your trying to display the data to the user, just cast the hash table using a LINQ statement and set the data source property. In fact using this method you can allow a privileged user to edit the value, which upon edit submit, you must update the hash table entry.

1 solution

If you need to display this information to some users then it would be best to cast and set a DataSource property of a useful control. In the example i used a data grid view since this is usually right for displaying table data.

Example for displaying:

C#
class Person {
     public string FirstName { get; set; }
     public string LastName { get; set; }
     public int Age { get; set; }
     public bool Current { get; set; }
}

HashTable table = new HashTable();
table["4874"] = new Person() {
   FirstName = "Berny",
   LastName = "Sanders",
   Age = 65,
   Current = true
};

table["6451"] = new Person() {
   FirstName = "Michelle",
   LastName = "Obama",
   Age = 50,
   Current = false
};

// The below assumes your using a data grid view with four columns, having the DataPropertyNames set to the same name as the Column name. If they are different, a new column will be added for each DataPropertyName not set.
dataGridView1.DataSource = table.Cast<DictionaryEntry>().Select(x => new {
    Column1_FirstName = ((Person)x.Value).FirstName,
    Column2_LastName = ((Person)x.Value).LastName,
    Column3_Age = ((Person)x.Value).Age,
    Column4_Current = ((Person)x.Value).Current
}).Reverse().ToList();

OUTPUT::

FirstName | LastName | Age | Current
Berny     | Sanders  | 65  |  True
Michelle  |  Obama   | 50  |  False

Note the use of Reverse().  Calling ToList() on a cast of the Hashtable will read from bottom up, so to keep the order (though the order is not guaranteed), call reverse().
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900