Reputation: 4995
i am developing an application in which i add controls during runtime and to keep track of these controls i am maintaing separate hashtables so that it will be easy for me to do what ever i want with them later. But I am having around 6 hashtables and maintaining these is a little complex. So i was wondering whether i can have all these controls in to one data structure but it should be easy for me to identify them just like i do in hashtable. Basically what i want is an extended version of hashtable where i can add multiple values to one key. For example what i have now is
hashtable addButtons = new hashtable();
hashtable remButtons = new hashtable();
addButtons.add(name,b);
remButtons.add(name,r);
Now it would be cool if i can do something like
addButtons.add(name=>(b,r,etc,etc));
and then get any of it just like in hashtable like
addButtons[name][1]
Can any one tell me if something like this is possible in c#.
Upvotes: 3
Views: 246
Reputation: 78447
Not sure I understood your question correctly. Something like this?
var controlsDictionary = new Dictionary<string, List<Control>>();
var b1 = new Button();
var b2 = new Button();
controlsDictionary["AddButtons"] = new List<Control> { b1, b2 };
var firstButton = controlsDictionary["AddButtons"][0];
Upvotes: 2
Reputation: 27339
Sounds like you want a Dictionary of Dictionaries, something like:
var d = new Dictionary<string, Dictionary<string, Control>>();
//Create new sub dictionary
d.Add("name1", new Dictionary<string, Control>());
//Add control
d["name1"].Add("btnOne", btnOne);
//Retrieve control
var ctrl = d["name1"]["btnOne"];
Upvotes: 1
Reputation: 1062502
I would have a class representing the 6 things...
public class SomeType { // RENAME ME
public int AddButton {get;set;}
public string RemoveButton {get;set;}
public DateTime Some {get;set;}
public float Other {get;set;}
public decimal Names {get;set;}
public bool Here {get;set;}
} // ^^ names and types above just made up; FIX ME!
and a Dictionary<string,SomeType>
- then you can:
yourField.Add(name, new SomeType { AddButton = ..., ..., Here = ... });
and
var remButton = yourField[name].RemoveButton;
Upvotes: 1
Reputation: 31642
How about Dictionary<String, List<?>>
?
Combined with something like this:
public static class Extensions
{
public static void AddItemsWithName<T>(this Dictionary<String, List<T>> this, string name, params T[] items)
{
// ...
}
}
Would give you something very close to the syntax you're looking for.
Upvotes: 3