Note that there are some explanatory texts on larger screens.

plurals
  1. POAssistance required with delegates example
    primarykey
    data
    text
    <p>Been trying to wrap my head around delegates, but always seem to hit a brick wall when it comes to it sinking in.</p> <p>I've tried to make my own example to help me understand, after reading an article about delegates -> <a href="http://www.codeproject.com/Articles/71154/C-Delegates-101-A-Practical-Example" rel="nofollow">http://www.codeproject.com/Articles/71154/C-Delegates-101-A-Practical-Example</a> but so far have been unsucessful. The following code produces an error about this line: 'doneExecuting = FunctionListToRun(MainOfWindows);' = Unassigned local variable</p> <p>Can someone tell me if I'm close and what I'm doing wrong?</p> <p>(I've also included the UI code on pastebin in case it helps -> <a href="http://pastebin.com/D2BVZJXc" rel="nofollow">http://pastebin.com/D2BVZJXc</a>)</p> <pre><code>public partial class MainWindow : Window { public delegate bool FunctionToCall(MainWindow windowInstance); public MainWindow() { InitializeComponent(); addMethodNames(); } private void addMethodNames() { ListBoxItem lbi1 = new ListBoxItem(); lbi1.Content = "Introduction"; lbi1.Name = "get" + lbi1.Content; listMethods.Items.Add(lbi1); ListBoxItem lbi2 = new ListBoxItem(); lbi1.Content = "Greeting"; lbi2.Name = "get" + lbi2.Content; listMethods.Items.Add(lbi2); ListBoxItem lbi3 = new ListBoxItem(); lbi3.Content = "Story"; lbi3.Name = "get" + lbi3.Content; listMethods.Items.Add(lbi3); ListBoxItem lbi4 = new ListBoxItem(); lbi4.Content = "MyName"; lbi4.Name = "get" + lbi4.Content; listMethods.Items.Add(lbi4); ListBoxItem lbi6 = new ListBoxItem(); lbi6.Content = "Conclusion"; lbi6.Name = "get" + lbi6.Content; listMethods.Items.Add(lbi6); } private void btnAddAction_Click(object sender, RoutedEventArgs e) { ListBoxItem lbi = (ListBoxItem)listMethods.Items[listMethods.SelectedIndex]; listMethods.Items.Remove(lbi); listActions.Items.Add(lbi); } private void btnRemoveAction_Click(object sender, RoutedEventArgs e) { listActions.Items.RemoveAt(listActions.SelectedIndex); } private void btnRun_Click(object sender, RoutedEventArgs e) { bool doneExecuting = false; FunctionToCall FunctionListToRun; foreach (ListBoxItem methodName in listActions.Items) { Conclusion conc = new Conclusion(); switch (methodName.Content.ToString()) { case "Introduction": Introduction intro = new Introduction(); FunctionListToRun = intro.getIntroduction; break; case "Greeting": Greeting greet = new Greeting(); FunctionListToRun = greet.getGreeting; break; case "Story": Story story = new Story(); FunctionListToRun = story.getStory; break; case "MyName": MyName name = new MyName(); FunctionListToRun = name.getName; break; case "Conclusion": FunctionListToRun = conc.getConclusion; break; default: FunctionListToRun = conc.getConclusion; break; } } doneExecuting = FunctionListToRun(MainOfWindows); } } class Introduction { public bool getIntroduction(MainWindow windowInstance) { windowInstance.txtResult.Text += " Hello there!"; return true; } } class Greeting { public bool getGreeting(MainWindow windowInstance) { windowInstance.txtResult.Text += " How are you today?"; return true; } } class Story { public bool getStory(MainWindow windowInstance) { windowInstance.txtResult.Text += " I once met a goat and his name was billy, and he lived on a plain that was very hilly."; return true; } } class MyName { public bool getName(MainWindow windowInstance) { windowInstance.txtResult.Text += " My name is too infinity!"; return true; } } class Conclusion { public bool getConclusion(MainWindow windowInstance) { windowInstance.txtResult.Text += " That is all, goodbye!"; return true; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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