2011-06-27 26 views
3

我正在开发一个应用程序,我在运行时添加控件,并跟踪这些控件我正在维护单独的哈希表,以便它可以很容易地做我想做的事情他们以后。但是我有大约6个哈希表,并且保持这些有点复杂。所以我想知道我是否可以将所有这些控件都放到一个数据结构中,但是我应该很容易地识别它们,就像我在散列表中一样。基本上我想要的是散列表的扩展版本,我可以在一个键上添加多个值。比如我现在已经是数据结构,以消除c中的多个哈希表#

hashtable addButtons = new hashtable(); 
hashtable remButtons = new hashtable(); 

addButtons.add(name,b); 
remButtons.add(name,r); 

现在它会很酷,如果我可以这样做

addButtons.add(name=>(b,r,etc,etc)); 

,然后得到任何它就像哈希表一样

addButtons[name][1] 

任何人都可以告诉我,如果这样的事情可能在C#中。

+0

我有点困惑。这是不是可以通过'Dictionary >'来实现吗? – user122211

回答

1

我会代表的6件事一类...

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! 

Dictionary<string,SomeType> - 那么你可以:

yourField.Add(name, new SomeType { AddButton = ..., ..., Here = ... }); 

var remButton = yourField[name].RemoveButton; 
+0

这一个看起来完全像我想要的...谢谢 – swordfish

3

Dictionary<String, List<?>>怎么样?

结合是这样的:

public static class Extensions 
{ 
    public static void AddItemsWithName<T>(this Dictionary<String, List<T>> this, string name, params T[] items) 
    { 
      // ... 
    } 
} 

会给你一些非常接近你要找的语法。

1

听起来像是你想词典词典,如:

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"]; 
2

不知道我正确理解你的问题。像这样?

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];