2017-12-08 8 views
1

私は製品電卓プログラムに取り組んでいます。アプリ内では、販売担当者がクライアントIDを検索することができ、アプリは彼がクライアントに提供できるサービスと販売のための提供を表示します。フォームはデータベースからダウンロードしたデータに基づいて生成されます。 今、私はリストに生成されたコントロールを格納しようとしています。検索が行われるたびに、私はコントロールを破棄し、リストをクリアします。私がうまくいかないことは、すべてのリストを単一の辞書に保存することです。コントロールの一般的なリストの辞書

このような何か...

public class ListOfControls<T> : IListOfControls<T> where T : Control 
{ 
    private readonly List<T> _controlsList; 

    public ListOfControls() 
    { 
     _controlsList = new List<T>(); 
    } 

    public void AddControll(T control) 
    { 
     _controlsList.Add(control); 
    } 

    public T this[int number] 
    { 
     get 
     { 
      return _controlsList[number]; 
     } 
    } 

    public void ClearControls() 
    { 
     _controlsList.Clear(); 
    } 

    public T Last() 
    { 
     return _controlsList.Last(); 
    } 
} 

class DictionaryOfControlsLists 
{ 
    //will be private - public only for test 
    public readonly Dictionary<string, IListOfControls<Control>> _dictionaryOfLists; 

    public DictionaryOfControlsLists() 
    { 
      _dictionaryOfLists = new Dictionary<string, IListOfControls<Control>>(); 
    } 

    //Other code.... 

} 

今すぐ実装しようと...

DictionaryOfControlsLists _testDict = new DictionaryOfControlsLists(); 
_testDict._dictionaryOfLists.Add("test", new ListOfControls<Label>()); 

は、悲しいことに、この文句を言わない仕事...任意のアイデア?あなたが必要なものTHANKS

+3

既存の 'Dictionary >'を使うのはなぜですか?それ以外の 'ListOfControls

+0

あなたはどんなエラーが出ていますか? DictionaryOfContorolsのAdd()メソッド実装を見せてください。 – Dragonthoughts

+0

私はそれらが同じ種類ではないことを知っていますが、私は辞書にさまざまなタイプのコントロールのリストを保存したいと思います。 – DavidWaldo

答えて

1

はこのようなものです:

class DictionaryOfControlsLists 
{ 
    private readonly Dictionary<Type, IListOfControls<Control>> _dictionaryOfLists = new Dictionary<Type, IListOfControls<Control>>(); 

    public void Add<T>(T control) where T : Control 
    { 
     if (!_dictionaryOfLists.ContainsKey(typeof(T))) 
     { 
      _dictionaryOfLists[typeof(T)] = new ListOfControls<Control>(); 
     } 
     _dictionaryOfLists[typeof(T)].AddControl(control); 
    } 

    public T Get<T>(int number) where T : Control 
    { 
     if (!_dictionaryOfLists.ContainsKey(typeof(T))) 
     { 
      _dictionaryOfLists[typeof(T)] = new ListOfControls<Control>(); 
     } 
     return _dictionaryOfLists[typeof(T)][number] as T; 
    } 
} 

次にあなたがこれを行うことができます:あなたはstring keyを持っているために、これを拡張する必要がある場合は

DictionaryOfControlsLists _testDict = new DictionaryOfControlsLists(); 
_testDict.Add<Label>(new Label()); 
Label label = _testDict.Get<Label>(0); 

を、あなたは、二重を実装する必要があります辞書をDictionaryOfControlsListsに入力してください。Dictionary<Type, Dictionary<string, IListOfControls<Control>>>のようなものです。

関連する問題