Delegate list and events?

This is a discussion on Delegate list and events? within the C# Programming forums, part of the General Programming Boards category; Working on a simple Windows form calculator to learn delegates. I have it set up when you click a button ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    42

    Delegate list and events?

    Working on a simple Windows form calculator to learn delegates. I have it set up when you click a button it adds that operation to my list. There is an input textbox that checks to see if the input is a double in another class. I want it to update the ouput each time I click a button or change the "input". I am just stuck and the calculation portion. Can anyone give me some guideance? I didn't put in all the code for all the different buttons to keep it short, but here is the general idea:

    Code:
     public partial class Form1 : Form
        {
            delegate double Operation(double Value); //declare delegate that takes a "double"
    
            public Form1()
            {
                InitializeComponent();
    
                double CurrentValue;
                try
                {
                    foreach (Operation O in this.Operators)
                    {
    
                        CurrentValue = O(doubleValueEdit1.Value);
                        if (CurrentValue < 0)
                            break;
                        tbOutput.Text = (CurrentValue.ToString("0.00"));
                    }
                }
                catch 
                {
                }
            }
    
                 List<Operation> Operators = new List<Operation>();
    
              private double Add1(double i)
            {
                return (i + 1);
            }
    
     private void bAdd1_Click(object sender, EventArgs e)
            {
                this.Operators.Add(new Operation(this.Add1));
                lActionTaken.Text = lActionTaken.Text + bAdd1.Text + ", ";
            }
    
    private void tbOutput_TextChanged(object sender, EventArgs e)
            {
               
            }
     
    private void doubleValueEdit1_TextChanged(object sender, EventArgs e)
            {
                
            }
    
        }
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    I'm not clear on what you're trying to accomplish, but the way you have it set up, you're only invoking the delegates in your list when the form is first being instantiated and the list is empty. When you get around to actually clicking the button, you add the operation to the list, but you're never invoking it.

    Are you trying to build up a queue of operations and then perform them all at once by cycling through the built-up queue? I'm just not seeing how the list fits into the whole picture.

    BTW, what type is doubleValueEdit1? Is that a custom control? If it's a NumericUpDown, the type for Value is decimal, so to pass it as a parameter to your operation you'll need to explicitly cast it as a double (i.e. CurrentValue = O((double)doubleVAlueEdit1.Value); )
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    yes, doubleValueEdit1 is a custom control derived from textbox. I am trying to update the output value based on my current list. So as I click a control or if I change the input textbox (doubleValueEdit1.Value) it will update the output textbox based on whats in the list.
    Last edited by d2rover; 03-17-2011 at 01:04 PM.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    How about something like this?
    Code:
            delegate double Operation(double Value); //declare delegate that takes a "double"
    
            private List<Operation> _Operators = new List<Operation>();
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private double Add1(double i)
            {
                return i + 1;
            }
    
            private void bAdd1_Click(object sender, EventArgs e)
            {
                AddOperation(new Operation(this.Add1));
            }
    
            private void AddOperation(Operation operation)
            {
                _Operators.Add(operation);
                UpdateOutput();
            }
    
            private void UpdateOutput()
            {
                StringBuilder labelText = new StringBuilder();
                double result = doubleValueEdit1.Value;
    
                foreach (Operation o in _Operators)
                {
                    labelText.Append("," + o.Method.Name);
                    result = o(result);
                }
    
                lActionTaken.Text = labelText.ToString().TrimStart(',');
                tbOutput.Text = result.ToString("0.00");
            }
    
            private void doubleValueEdit1_TextChanged(object sender, EventArgs e)
            {
                UpdateOutput();
            }
        }
    I added a couple of helper functions to make adding operations and updating the output easier since you'll have to do it for each button. And then, since you're not storing the button anywhere, there's no way to get its name while building the label text. You could just build the label text after each operation I guess, but I opted to use reflection instead. It's up to you.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    Thank you! The helper functions made all the difference! That is where I was having the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reliable UDP communication for merging events in a distributed system
    By dwks in forum Networking/Device Communication
    Replies: 1
    Last Post: 12-26-2009, 04:36 PM
  2. Event driven game engine
    By abachler in forum Game Programming
    Replies: 9
    Last Post: 12-01-2009, 05:03 AM
  3. Events
    By Dae in forum C++ Programming
    Replies: 0
    Last Post: 07-01-2009, 03:54 AM
  4. Instant messenger app help
    By AusTex in forum C Programming
    Replies: 2
    Last Post: 05-01-2005, 12:41 AM
  5. Logic problem adding nodes to linked list
    By SeanMSimonsen in forum C++ Programming
    Replies: 0
    Last Post: 04-02-2003, 06:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21