Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to call an action stored in Dictionary?
    primarykey
    data
    text
    <p>I am attempting to setup a <code>dictionary</code> that will then have its <code>keys</code> stored as <code>items</code> in a <code>listbox</code>. </p> <p>I have been able to establish a <code>dictionary</code> that then has its <code>keys</code> entered in the <code>listbox</code>, but I'm not sure how to then execute the action associated with the <code>key</code>. From the previous thread there was a recommendation, but I've ran into issues with it:<a href="https://stackoverflow.com/questions/14244455/how-to-store-a-list-of-cases-from-switch-statement#comment19766478_14244455"> Original Thread </a> </p> <pre><code>Dictionary&lt;string, Action&gt; dict = new Dictionary&lt;string, Action&gt;(); 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(); } </code></pre> <p>I believe that my code above is mostly correct and that I'm understanding it, however the action line: </p> <pre><code>var action = dict[n] as Action action(); </code></pre> <p>Shows an error stating 'action' is expecting a <code>';'</code>. Is my logic here accurate? If so, why is the action call incorrect?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload