を設定しようとしてdictionary
います。これは、のようにkeys
保存されitems
ますlistbox
。
を確立しdictionary
て、にkeys
入力することはできましたlistbox
が、に関連付けられたアクションを実行する方法がわかりませんkey
。前のスレッドから推奨事項がありましたが、問題が発生しました:元のスレッド
Dictionary<string, Action> dict = new Dictionary<string, Action>();
public void SetDictionary()
{
//add entries to the dictionary
dict["cat"] = new Action(Cat);
dict["dog"] = new Action(Dog);
//add each dictionary entry to the listbox.
foreach (string key in dict.Keys)
{
listboxTest.Items.Add(key);
}
}
//when an item in the listbox is double clicked
private void listboxTest_DoubleClick(object sender, EventArgs e)
{
testrun(listboxCases.SelectedItem.ToString());
}
public void testrun(string n)
{
//this is supposed to receive the item that was double clicked in the listbox, and run it's corresponding action as defined in the dictionary.
var action = dict[n] as Action action();
}
上記の私のコードはほとんど正しく、理解していると思いますが、アクションラインは次のとおりです。
var action = dict[n] as Action action();
'action'がを期待していることを示すエラーを示します';'
。ここでの私の論理は正確ですか?もしそうなら、なぜアクションコールが正しくないのですか?