473,761 Members | 8,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to explicitly implement an interface event

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: oh****@bgumail. bgu.ac.il
Nov 15 '05 #1
3 8216
Try this.

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

--
Jared Parsons [MSFT]
ja******@online .microsoft.com
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.c om/info/cpyright.htm"

"Ohad Young" <oh****@bgumail .bgu.ac.il> wrote in message
news:OZ******** *******@TK2MSFT NGP11.phx.gbl.. .
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: oh****@bgumail. bgu.ac.il

Nov 15 '05 #2

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/de...us/cscomp/html
/vcerrCompilerEr rorCS0071.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.EventHan dler 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/de...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/de...us/csref/html/
vclrfeventpg.as p

=============== =============== =============== ===============
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.

Nov 15 '05 #3

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.

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
17195
by: Peter | last post by:
I want to copy a parent class instance's all datas to a child's. It's actually a C++'s copy constructor. But why the following code does not work - there is a compile error! How it should look like? (The background is I don't know (I don't care indeed) all members in DataGrid, so I don't want to copy all members in DataGrid one by one.) public class GridEx : DataGrid { public GridEx()
3
2546
by: Guthrie Phalanx | last post by:
HI all Bit of a newbie on interfaces, I hope this is not a silly question. I am stuck on a problem where I want to create a custom collection of objects, all of whom implement the same interface. The following code probably explains my situation better than words do : interface IEvent { string Title {get; set;} string Description {get; set;}
2
4990
by: Alex Sedow | last post by:
Why interface-event-declaration does not support multiple declarators like event-declaration? Grammar from C# spec: variable-declarators: variable-declarator variable-declarators "," variable-declarator variable-declarator:
3
2725
by: Brett Hall | last post by:
I have a VB.NET interface that my managed C++ code is to implement. I seem to be stuck implementing an event defined in that interface. Does anyone have a simple code snippet that will show me the basics of what I need to implement? I've seen all the MSDN articles on implementing events in managed C++ and I've gotten events to work without issue when implementing all the constructs
7
1817
by: Jay Douglas | last post by:
Greetings, I have a Windows form application that (naturally) instantiates all sorts of objects. I have a base object that contains an event. Lots of other objects inherit from this event. When the base object or any derived object is instantiated I would like to automagically start listening for the event. An example and a further explanation follows: public delegate NeedFoodDelegate(object sender, EventArgs args);
6
2444
by: keith.thornhill | last post by:
hi all, lets say i have a usercontrol which implements a custom interface. like so: ------------------------------------------------- interface IMyInterface sub buttonClick() end interface
6
4810
by: Smithers | last post by:
Just looking to compile a list of "all the ways to implement events". I'm NOT looking to get into the merits or mechanics of each in this thread... just want to identify them all - good, bad, and ugly. Here's what I have so far 1. Implement via the 'event' keyword (with associated delegate, etc) 2. Expose a private delegate via a public property (really a "roll yer own" version of the 'event' keyword method).
5
2413
by: Tony Johansson | last post by:
Hello! Assume you have the following interface and classes shown below. It is said that a class must implement all the methods in the interface it inherits. Below we have class MyDerivedClass that inherits IMyInterface but MyDerivedClass doesn't implement method DoSomething() it inherits it from the base class MyBaseClass. So the statement that a class must implement all method in an interface that
5
2658
by: Tony Johansson | last post by:
Hello! The only reason I can see interface to be implemented explicitly is when the a class implement two interface having the same method signature. In all other cases I can implement interface implicitlly. Can you agree with me about this statement ? //Tony
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8780
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5229
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.