First of all, it causes the stack overflow exception if you try to add
something to TestStatusChanged. Because by the code below you simple
redefine the default "event" template behavior to add the value to
itself up to overflow exception.
public event TestStatus TestStatusChanged
{
add { TestStatusChanged += value; }
remove { TestStatusChanged -= value; }
}
So, if you want your code to work then rewrite it just as:
public event TestStatus TestStatusChanged;
or like:
class Test : ITest
{
private TestStatus _testStatus;
public event TestStatus TestStatusChanged
{
add { _testStatus += value; }
remove { _testStatus -= value; }
}
}
But calling the _testStatus(..), not the TestStatusChanged(..) in this
case. Because it's what the "event" statement do behind
(
http://www.codeproject.com/useritems/howeventswork.asp).
Regards, Alex Meleta
Blog::
http://devkids.blogspot.com
-----Original Message-----
From:
hu*******@yahoo.com [mailto:hu*******@yahoo.com]
Posted At: Montag, 30. April 2007 00:25
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Interface & Event
Subject: Interface & Event
I have an interface ITest that includes an event TestStatusChange.
There is also a class Test that implements ITest. In one of the
functions of Test I want to call the event (see code at the end) but
get the following error:
"The event 'eventTest.Test.TestStatusChanged' can only appear on the
left hand side of += or -=.
All samples I saw seem to do the same I am doing. What am I missing?
Thanks
using System;
using System.Collections.Generic;
using System.Text;
namespace eventTest
{
public delegate void TestStatus(String status);
interface ITest
{
event TestStatus TestStatusChanged;
}
class Test : ITest
{
public event TestStatus TestStatusChanged
{
add { TestStatusChanged += value; }
remove { TestStatusChanged -= value; }
}
public void Check()
{
TestStatusChanged("ok"); //!!!!!!!!! COMPILE
ERROR !!!!!!!!!!!!!
}
}
class Program
{
static void Main(string[] args)
{
}
}
}