G
Guest
When I override the 'add' and 'remove' methods for an event, why can't I
invoke it ? The sample code is provided below - I have two events - 'my' and
'ev' - while invoking, my() works but ev() does not ! The compiler complains
that : The event "'Blog.EventsvsDelegates.Form1.ev' can only appear on the
left hand side of += or -="
Why ?
public delegate void MyEvent();
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private event MyEvent my;
private event MyEvent pvt_ev;
public event MyEvent ev
{
add
{
pvt_ev +=value;
}
remove
{
pvt_ev-=value;
}
}
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
my+=new MyEvent(this.Test);
my+=new MyEvent(this.Test1);
ev+=new MyEvent (this.Test );
}
protected void Test()
{
MessageBox.Show ("Test");
}
protected void Test1()
{
MessageBox.Show ("Test1");
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
my(); // WORKS
//ev (); //DOES NOT WORK
}
}
invoke it ? The sample code is provided below - I have two events - 'my' and
'ev' - while invoking, my() works but ev() does not ! The compiler complains
that : The event "'Blog.EventsvsDelegates.Form1.ev' can only appear on the
left hand side of += or -="
Why ?
public delegate void MyEvent();
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private event MyEvent my;
private event MyEvent pvt_ev;
public event MyEvent ev
{
add
{
pvt_ev +=value;
}
remove
{
pvt_ev-=value;
}
}
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
my+=new MyEvent(this.Test);
my+=new MyEvent(this.Test1);
ev+=new MyEvent (this.Test );
}
protected void Test()
{
MessageBox.Show ("Test");
}
protected void Test1()
{
MessageBox.Show ("Test1");
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
my(); // WORKS
//ev (); //DOES NOT WORK
}
}