473,757 Members | 10,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Explicit Interface Implementation And Events

Hi All,

I've got a situation where I'm implementing an interface (BaseInterface in
example below) and I want to use explicity interface implementation of an
event so that I can add type safety. To see what I mean look at the example
below where the class implementing the interface actually wants the event to
be for a more specific delegate.

Now this seems to work but the code, to me is unnecessarily ugly. This
leaves me wondering if this is something other people do and if so is there
a better way of doing it?

I should also say the reason I'm doing it is in a real situation I want to
have a class implementing an interface raising the event with EventArguments
that are derived from the ones that the delegate in the interface uses,
doing so stops me having to cast the event arguments in the event handler.

Thanks in advance,

Colin Jack
public delegate void GeneralEventHan dler(object sender, GeneralEventArg s
ex);
public delegate void SpecificEventHa ndler(object sender, SpecificEventAr gs
ex);

public class GeneralEventArg s : EventArgs
{
public string general = "General";
}

public class SpecificEventAr gs : GeneralEventArg s
{
public string specific = "Specific";
}
public interface BaseInterface
{
event GeneralEventHan dler GeneralEvent;
}

public class ConcreteClassSp ecific: BaseInterface
{
// explicit interface implementation, I need to actually specify
// the add/remove accessors
GeneralEventHan dler specificEvent;
event InheritanceTest .GeneralEventHa ndler BaseInterface.G eneralEvent
{
add
{
specificEvent += value;
}
remove
{
specificEvent -= value;
}
}

// This means that subscribers have to use SpecificEventHa ndler which
// allows them to access its specific string field
public event SpecificEventHa ndler GeneralEvent;

protected void OnEvent()
{
if (specificEvent != null)
{
specificEvent(n ull, new GeneralEventArg s());
}

if (GeneralEvent != null)
{
GeneralEvent(th is, new SpecificEventAr gs());
}
}

public void RaiseEvent()
{
OnEvent();
}
}
Nov 17 '05 #1
2 2513
I don't see your implementation as necessarily, "sloppy", however the design patter for delegates states the use of "object" as
sender and "EventArgs" as the args for the fact that certain events may require usage of different "senders" and "args" which these
base types will allow via inheritance. To prevent having to cast, I think your solution is fine.

I'd also like to say that it may not be the best design to have an interface event "overriden" . The purpose of an Interface is to
provide a contract, but your breaching this contract. Maybe you should have a different event on the implementing type named,
"SpecificEv ent" along side of the implementation for "GeneralEve nt". It's hard to say without knowing the business logic behind the
events themselves.

BTW, your accessors are using "specificEvent+ =" when they should be using "GeneralEvent+= ", correct? Maybe just an oversight:
event InheritanceTest .GeneralEventHa ndler BaseInterface.G eneralEvent
{
add
{
specificEvent += value;
}
remove
{
specificEvent -= value;
}
}

--
Dave Sexton
dave@www..jwaon line..com
-----------------------------------------------------------------------
"COLIN JACK" <cj********@blu eyonder.co.uk> wrote in message news:wK******** *********@fe2.n ews.blueyonder. co.uk... Hi All,

I've got a situation where I'm implementing an interface (BaseInterface in example below) and I want to use explicity interface
implementation of an event so that I can add type safety. To see what I mean look at the example below where the class
implementing the interface actually wants the event to be for a more specific delegate.

Now this seems to work but the code, to me is unnecessarily ugly. This leaves me wondering if this is something other people do
and if so is there a better way of doing it?

I should also say the reason I'm doing it is in a real situation I want to have a class implementing an interface raising the
event with EventArguments that are derived from the ones that the delegate in the interface uses, doing so stops me having to cast
the event arguments in the event handler.

Thanks in advance,

Colin Jack
public delegate void GeneralEventHan dler(object sender, GeneralEventArg s ex);
public delegate void SpecificEventHa ndler(object sender, SpecificEventAr gs ex);

public class GeneralEventArg s : EventArgs
{
public string general = "General";
}

public class SpecificEventAr gs : GeneralEventArg s
{
public string specific = "Specific";
}
public interface BaseInterface
{
event GeneralEventHan dler GeneralEvent;
}

public class ConcreteClassSp ecific: BaseInterface
{
// explicit interface implementation, I need to actually specify
// the add/remove accessors
GeneralEventHan dler specificEvent;
event InheritanceTest .GeneralEventHa ndler BaseInterface.G eneralEvent
{
add
{
specificEvent += value;
}
remove
{
specificEvent -= value;
}
}

// This means that subscribers have to use SpecificEventHa ndler which
// allows them to access its specific string field
public event SpecificEventHa ndler GeneralEvent;

protected void OnEvent()
{
if (specificEvent != null)
{
specificEvent(n ull, new GeneralEventArg s());
}

if (GeneralEvent != null)
{
GeneralEvent(th is, new SpecificEventAr gs());
}
}

public void RaiseEvent()
{
OnEvent();
}
}

Nov 17 '05 #2
Hi Dave,

Thanks for the reply, very useful to read someone else's opinion.

First off yes using the "specificEv ent" was an oversight, I should have
read through the code before posting :)

However I dont see that i'm breaking the contract. I say this because
you can still subscribe to and receive the GeneralEvent using the
GeneralEventHan dler delegate if you cast the publisher
(ConcreteClassS pecific) to the interface(BaseI nterface). However if
you, as a subscriber, want the more specific event arguments then you
subscribe to the event using a normal reference to the object (using a
ConcreteClassSp ecific reference). I've tried to show this in the
attached snippet.

I may be wrong but I see this as the same as using explicit interface
implementation to provide type safety with methods.

Your right about me trying to avoid following the normal event desing
pattern as I dont like the idea that I'm raising an event and the
receiver actually knows that the event args are a derived class so they
do a cast.

Ta,

Colin
private void SubscribeToEven ts()
{
ConcreteClassSp ecific myObject = new ConcreteClassSp ecific();
myObject.Genera lEvent += new
SpecificEventHa ndler(specific_ GeneralEvent);

BaseInterface interfaceBase = (BaseInterface) myObject;
interfaceBase.G eneralEvent +=new
GeneralEventHan dler(interfaceB ase_GeneralEven t);

myObject.RaiseE vent();
}

private void specific_Genera lEvent(object sender, SpecificEventAr gs
ex)
{
}

private void interfaceBase_G eneralEvent(obj ect sender,
GeneralEventArg s ex)
{
}

Dave wrote:
I don't see your implementation as necessarily, "sloppy", however the design patter for delegates states the use of "object" as sender and "EventArgs" as the args for the fact that certain events may require usage of different "senders" and "args" which these base types will allow via inheritance. To prevent having to cast, I think your solution is fine.
I'd also like to say that it may not be the best design to have an interface event "overriden" . The purpose of an Interface is to provide a contract, but your breaching this contract. Maybe you should have a different event on the implementing type named, "SpecificEv ent" along side of the implementation for "GeneralEve nt". It's hard to say without knowing the business logic behind the events themselves.

BTW, your accessors are using "specificEvent+ =" when they should be using "GeneralEvent+= ", correct? Maybe just an oversight:
event InheritanceTest .GeneralEventHa ndler BaseInterface.G eneralEvent {
add
{
specificEvent += value;
}
remove
{
specificEvent -= value;
}
}

--
Dave Sexton
dave@www..jwaon line..com

----------------------------------------------------------------------- "COLIN JACK" <cj********@blu eyonder.co.uk> wrote in message

news:wK******** *********@fe2.n ews.blueyonder. co.uk...
Hi All,

I've got a situation where I'm implementing an interface (BaseInterface in example below) and I want to use explicity interface implementation of an event so that I can add type safety. To see what I mean look at the example below where the class implementing the interface actually wants the event to be for a more specific delegate.
Now this seems to work but the code, to me is unnecessarily ugly. This leaves me wondering if this is something other people do and if so is there a better way of doing it?

I should also say the reason I'm doing it is in a real situation I want to have a class implementing an interface raising the event with EventArguments that are derived from the ones that the delegate in the interface uses, doing so stops me having to cast the event arguments in the event handler.

Thanks in advance,

Colin Jack
public delegate void GeneralEventHan dler(object sender, GeneralEventArg s ex); public delegate void SpecificEventHa ndler(object sender, SpecificEventAr gs ex);
public class GeneralEventArg s : EventArgs
{
public string general = "General";
}

public class SpecificEventAr gs : GeneralEventArg s
{
public string specific = "Specific";
}
public interface BaseInterface
{
event GeneralEventHan dler GeneralEvent;
}

public class ConcreteClassSp ecific: BaseInterface
{
// explicit interface implementation, I need to actually specify
// the add/remove accessors
GeneralEventHan dler specificEvent;
event InheritanceTest .GeneralEventHa ndler BaseInterface.G eneralEvent {
add
{
specificEvent += value;
}
remove
{
specificEvent -= value;
}
}

// This means that subscribers have to use SpecificEventHa ndler which // allows them to access its specific string field
public event SpecificEventHa ndler GeneralEvent;

protected void OnEvent()
{
if (specificEvent != null)
{
specificEvent(n ull, new GeneralEventArg s());
}

if (GeneralEvent != null)
{
GeneralEvent(th is, new SpecificEventAr gs());
}
}

public void RaiseEvent()
{
OnEvent();
}
}


Nov 17 '05 #3

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

Similar topics

12
2804
by: Steve W. | last post by:
I just read the section (and did the exercise) in the C# Step by Step book that covers Explict Interface Implementation (where you specify in the method implementation the specific interface that you are implementing in the class. Other than to resolve the problem that arises when a class implements two interfaces with the same method signature, what good is it?
2
1602
by: cody | last post by:
it seems to me that explicit interface implementation does not work with events: class Foo : IContentchangeableControl { event EventHandler MyNs.IContentchangeableControl.ContentChanged; } yields to CS0071 and CS1519.
4
2380
by: Mathieu Cartoixa | last post by:
Hi, I have been annoyed in one of my recent projects with a problem related to the explicit implementation of an interface on a value type. I will take an example to show the problem. Say we have this simple interface : interface IInitializable { void Init();
14
4064
by: Noone | last post by:
Hello all, Ok, I want to create a program that will load plugins (dll's) from a plugin folder. I can create the forms and put them into a dll but I cannot actually add them dynamically at run time. I have tried to use the LoadLibrary and GetProc functions, which sort of worked. I got the pointer to the function but I cannot actually RUN the function like I can in C++. I have heard some things about Invoking(?) I believe but I cannot...
0
2221
by: Ken | last post by:
Hi I have a little application that does datavalidation. It supports dynamically loaded plugins (you drop a dll with a class implementing IValidator<Tin the same dir as the main application). All classes that implement this interface are shown in a drop down in a DataGridViewComboBoxColumn named Validator. By choosing the class to validate this specific data in the drop downbox IValidator<T>.Validate is called and returns true/false....
0
1244
by: Brandon Driesen | last post by:
The following illustrates my question. Why is it when I bind to an a collection of items whose interface implementation is explicit, there is an error during the binding process wherein the error is as follows: A field or property with the name 'FirstName' was not found on the selected data source. Seems to me there is a shortcoming of the Binding Process to Web Controls. There should not be any impediment doing what I am doing since...
1
2760
by: recherche | last post by:
Hola! I tried the following public implementation of interface indexer by struct (Code Snippet 1) in private and explicit implementation by struct (Code Snippet 2) but in vain. Please help! Code Snippet 1:
1
3222
by: =?Utf-8?B?Sk0=?= | last post by:
In an application I have an interface with methods and properties. The interface is used on a Class (ie class MyClass : IMyClassA, IMyClassB). On a windows form I define a BindingSource (_bindingMyClass) where I set the DataSource property to the interface IMyClass. Now when the MyClass object is instantiated I set the BindingSource to the instance of the IMyClass interface (_MyClass) _bindingMyClass.DataSource = this._MyClass;
2
1907
by: puzzlecracker | last post by:
I don't see the purpose of explicit interface implementation other than to hide its signature in the class that implements it, and, instead, write your own, perhaps with a different signature, implementation. Also, if you implement more than two interfaces with the same method, which is rarity in practice or so it seems. To me it appears a shallow end... Am I missing something? Thanks
0
10072
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...
0
9906
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8737
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...
1
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6562
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
5172
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
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.