Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to store a list of Cases from Switch Statement?
    primarykey
    data
    text
    <p>In my program I have a listbox that when the user double clicks an object it looks to a switch statement to see what event should occur. As the list begins getting larger I'm curious if there is a way to avoid having to maintain the list of objects in 2 places (once in a list to Add to the listbox, and once in the switch statement. Is there a way to index/read/store the various Cases of my switch statement, then add them as objects to my listbox?</p> <p>Example: (doesn't work, just a theory)</p> <pre><code>Switch (n) ForEach (Case c in Cases) { arrayCases.Add(c); } listbox.Items.AddRange(arrayCases); </code></pre> <p>EDIT:</p> <p>Going on the Dictionary recommendations I now have:</p> <pre><code>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: var action = dict[n] as Action action();</p> <p>Shows an error stating 'action' is expecting a ';'. 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.
 

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