I am attempting to setup a dictionary
that will then have its keys
stored as items
in a listbox
.
I have been able to establish a dictionary
that then has its keys
entered in the listbox
, but I'm not sure how to then execute the action associated with the key
. From the previous thread there was a recommendation, but I've ran into issues with it: Original Thread
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();
}
I believe that my code above is mostly correct and that I'm understanding it, however the action line:
var action = dict[n] as Action action();
Shows an error stating 'action' is expecting a ';'
. Is my logic here accurate? If so, why is the action call incorrect?