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.
Eventclass (i'd rename this btw) what is it you're trying to achieve?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!