1

I have two .cs files. I'm trying to use event delegate in this project. Event.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project3
{
    public delegate int Calculate(int obj1, int obj2);
    public class Event
    {
        public event Calculate CalculateNow;
        int a = 0;
        int b = 0;
        int r = 0; 
        public int Add()
        {
            r = a + b;
            return r; 
        }

        public int Sub()
        {
            r = a - b;
            return r; 
        }

        public int Mul()
        {
            r = a * b;
            return r; 

        }

        public int Div()
        {
            r = a / b;
            return r; 
        }
    }
}

And Form1.cs:

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Project3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label1.Text = "";
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            label1.Text = btn.Text;
            int num1 = Int32.Parse(textBox1.Text);
            int num2 = Int32.Parse(textBox2.Text);
            Event t = new Event();
            t.CalculateNow +=new Calculate(t_CalculateNow);
            int finalr = t.Add();
            if (finalr != null)
            {
                label1.Text = "" + finalr;
            }
            else
            {
                label1.Text = "Error!";
            }
        }
    }
}

I got a error said"The name 't_CalculateNow' does not exist in the current context", and I don't know how to correct it. Hope someone can answer me. Thank you so much! If someone can explain the logic of event delegate, that will help me a lot.

5
  • Well, it doesn't exist in that context, its in the Event class (i'd rename this btw) what is it you're trying to achieve? Commented Aug 10, 2015 at 6:43
  • Google can explain the logic of event delegates. Amazon is also a useful resource, particularly if you like printed material. Commented Aug 10, 2015 at 6:45
  • @Sayse The event will do the calculation and return the final answer. The label1 will show the answer in the web form. Commented Aug 10, 2015 at 6:52
  • It doesn't really sound like you need an event at all here. If anything a static method would do the job. (If you really want to do this in a separate method) Commented Aug 10, 2015 at 6:55
  • @Sayse Right, but I'm trying to learn event delegate. If I want to apply that to this project, how can I change it? Hope you can give me an example. Thank you! Commented Aug 10, 2015 at 7:00

1 Answer 1

1

Your code is missing the correct event initialization and use. Event cannot be directly invoked from some other class however it's possible to create method that will invoke it for public subscribers (in our case it's InvokeCalc method). It looks like something like this shoukd work for you :

public class Calculator
{
    public event Calculate CalculateNow;
    public delegate int Calculate(int x, int y);

    public string InvokeCalc(int x, int y)
    {
        if(CalculateNow != null) return CalculateNow(x, y).ToString();
        return null;
    }
}

And then in your winform you can use it :

namespace Project3
{
    public partial class Form1 : Form
    {
        private Calculator calc;

        public Form1()
        {
            InitializeComponent();
            calc = new Calculator();
            label1.Text = "";
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            label1.Text = btn.Text;
            int num1 = Int32.Parse(textBox1.Text);
            int num2 = Int32.Parse(textBox2.Text);

            // subscribes a handler to your event
            calc.CalculateNow += (n1, n2) => { return n1 + n2; }

            // method that invokes your event on Calculator class
            int finalr = calc.InvokeCalc(num1, num2);
            if (finalr != null)
            {
                label1.Text = "" + finalr;
            }
            else
            {
                label1.Text = "Error!";
            }
        }
    }
}

P.S. Although it is an example that illustrates how you can use events but it's very unlikely that you'd have to use events this way. Generally, events should be used to notify subscribers (other classes) that event has occurred. Event can only be fired from inside the class and this encapsulation of some sort improves the integrity of the application because otherwise ANY class or struct can fire this event and it would be difficult to find out which class has actually invoked it and where...

Sign up to request clarification or add additional context in comments.

2 Comments

I was testing your code, but there is a error: The name 'Calculate' does not exist in the current context.
@Alison excuse me i had confused delegate name Calculate with event name CalculateNow... I had corrected this little confusion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.