-4

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?

Community
  • 1
  • 1
Fuzz Evans
  • 2,893
  • 11
  • 44
  • 63

3 Answers3

10

You're missing a ;:

var action = dict[n] as Action; action();
                              ↑
dtb
  • 213,145
  • 36
  • 401
  • 431
7

First off, I'm assuming the definition of the dictionary, since it wasn't listed is the following:

Dictionary<string, Action> dict;

Please indicate what the definition is if that doesn't match.

To execute the action for a given key all you need is:

dict[key]();

or

dict[key].Invoke();

To store it as a variable you (shouldn't) need a cast at all:

Action action = dict[key];

If you do need to cast it (meaning your dictionary definition differs from what I listed), you can do so like this:

Action action = dict[key] as Action;

You can then invoke it as shown above:

action();

or

action.Invoke();
Servy
  • 202,030
  • 26
  • 332
  • 449
  • No need to use as operator, can straight away invoke the Action dict[key](); – Nasmi Sabeer Jan 10 '13 at 18:16
  • @NasmiSabeer Did you read my answer? I started off saying that right off of the bat. When I got to the `as` operator I specifically stated it would only be appropriate if the signature of the `Dictionary` didn't match, i.e. if it was a `Dictionary` that happened to contain `Action` objects as the value. – Servy Jan 10 '13 at 18:18
  • sorry, my bad didn't read the answer in full – Nasmi Sabeer Jan 11 '13 at 07:51
1

Your testrun should be

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.
     dict[n]();
}

Based on the assumption your dictionary is Dictionary<string, Action> as @Servy suggested

Nasmi Sabeer
  • 1,370
  • 9
  • 21