Reputation: 105
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.
Upvotes: 1
Views: 1805
Reputation: 13676
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...
Upvotes: 1