How to explicitly implement an interface event

O

Ohad Young

Hi,

I have an interface with an event.
I'd like to explicitly implement the interface by a certain class.
However, I received the following error:
"An explicit interface implementation of an event must use property syntax"

Here is a code sample:

public interface IFoo
{
event EvenetHandler MyEvent;
}

public class MyClass : IFoo
{
...
event EvenetHandler IFoo.MyEvent;
}

Please advise, Ohad

--
Ohad Young
Medical Informatics Research Center
Ben Gurion University
Information System Eng
Office Phone: 972-8-6477160
Cellular Phone: 972-54-518301
E-Mail: (e-mail address removed)
 
J

Jared Parsons [MSFT]

Try this.

public class MyClass : IFoo
{
event EventHandler IFoo.MyEvent
{
add { ... }
remvoe { ... }
}
}

--
Jared Parsons [MSFT]
(e-mail address removed)
This posting is provided "AS IS" with no warranties, and confers no rights.
OR if you wish to include a script sample in your post please add "Use of
included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm"
 
G

Guest

Hi Ohad,

Thank you for posting in the community!

Based on my understanding, you want to explicit implement an interface
which takes an event field.

==========================================
Actually, when implementing an event that was declared in an interface, you
must use property syntax, which takes accessor(For event, they are "add"
and "remove").
Please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cscomp/html
/vcerrCompilerErrorCS0071.asp

Also, you can implicit implement an interface without using the prooperty
syntax:
public interface IFoo
{
event EventHandler MyEvent;
}

public class MyClass: IFoo
{
public event System.EventHandler MyEvent;
}

That is because implicit implemented fields are part of the CLASS, while
the explicit implemented fields are part of the INTERFACE.
For a class, use compiler will generate code for field like event:

class X {
public event D Ev;
}

could be compiled to something equivalent to:
class X {
private D __Ev; // field to hold the delegate
public event D Ev {
add {
lock(this) { __Ev = __Ev + value; }
}
remove {
lock(this) { __Ev = __Ev - value; }
}
}
}

For more information, please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html
/vclrfcsharpspec_10_7.asp

While as a field of interface, compiler will not do this for you, so you
must explicit the event through property syntax(Just as Jared pointed out):
public class MyClass : IFoo
{
event EventHandler IFoo.MyEvent
{
add { ... }
remvoe { ... }
}
}

Also, you may find more information about implement the event through:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/
vclrfeventpg.asp

============================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Guest

Hi Ohad,

Does my reply make sense to you?

If you still have anything unclear, please feel free to tell me, I will
help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top