C# - Adding event delegate declarations to interfaces

G

Guest

Hi all,

I want to be able to hirearchally define event delegate (declarations)
WITHIN interfaces.

Unfortunatelly C# 1 doesn't appear to support it.

How does the following look as a work-around? Are there better ways of
doing this?

namespace ITestInterface
{
delegate EventHandler(object Sender, string StatusInfo); // delegate
declaration

interface ClassTemplate
{
bool SampleProperty {get;}
void SampleMethod(int SomeData);
event EventHandler SampleEventHandlerList;
}
}

// Later on we could define
// a client and server that use the interface

using ITestInterface;

namespace ABC
{
class TestInterfaceServer: ITestInterface.ClassTemplate
{
public bool SampleProperty {get {return true;}}

public event EventHandler SampleEventHandlerList;

protected virtual void FireSampleEvents()
{
SampleEventHandlerList(this, "Hi there");
}

public void SampleMethod(int SomeData)
{
FireSampleEvents();
}
}

class TestInterfaceClient
{
ITestInterface.EventHandler testInterfaceEventHandler_;
TestInterfaceServer ourServer = new TestInterfaceServer();

void TestInterfaceClient()
{
testInterfaceEventHandler_ =
new ITestInterface.EventHandler(onTestInterfaceEvent);

TestInterfaceServer.SampleEventHandlerList +=
testInterfaceEventHandler_;

// Loop back test

TestInterfaceServer.SampleMethod(1); // .. should fire callback
}

onTestInterfaceEvent(object Sender, string StatusInfo)
{
// Handler for ITestInterface.EventHandler
}
}
}
 
B

Barry Kelly

steve said:
I want to be able to hirearchally define event delegate (declarations)
WITHIN interfaces.

Unfortunatelly C# 1 doesn't appear to support it.

What exactly does this mean? What is hierarchical?

Do you mean that the declaration of the delegate type is lexically
nested inside the interface? You can't do that, but it wouldn't give you
anything anyway - it would just mean that every client of the delegate
type would have to fully qualify the delegate type, which would be a
pain for all the users.
How does the following look as a work-around? Are there better ways of
doing this?

I'm not sure why you seem to want to lexically enclose types inside your
interface type - if that's the functionality you're after. Nested types
aren't a common pattern in C#. They are common in C++ for enumeration
types, for example, but that's because C++ enumeration types don't need
to be qualified by the enumeration name.

C# doesn't have this feature/flaw, so nested types are generally only
required when the inner type needs access to private and protected
fields of the enclosing type. Since interfaces can only have public
members, there isn't a need for types nested inside interfaces.

-- Barry
 
T

Tom Spink

steve said:
Hi all,

I want to be able to hirearchally define event delegate (declarations)
WITHIN interfaces.
<snippedy-doo-dah>

Hi Steve,

Why would you want to do such a thing; it doesn't make sense to define a
delegate within an interface... does it? Could you post your reasoning for
defining a delegate within an interface, please?
 
G

Guest

Thanks to all respondents. This topic is a bit of mental exploration for me.
So my thoughts on the matter haven't fully matured as yet.
I appreciate proving ideas with appropriate comments and critisms.

// Prelude

C# (1.0) interfaces allow you to declare the following

- properties
- methods
- events

Properties are fairly self explanatory
But methods and events can be seen in a number of different lights.

// What if a data-flow model is intended??

If we assume that an interface is designed with a data-flow paradigm in mind,
methods and events could be considered the forward and reverse flows of data
to and from the implementer of the interface.

C# method declarations are straightforward, but event declarations pose a
few syntatical problems (to my mind).

// C# syntatic sugar

A C# event has a general form of

Handler(object sender, EventArgDescendent e);

That's fine and dandy, but it requires a delegate type declaration, eg.

delegate SomeKindOfSpecialHandler(object sender, EventArgDescendent e);

// Problems ??

Since you can't put this type declaration inside an interface, it has to
float around inside some other namespace (or class). This is a problem to
me, since

(a) Many of the events I define are very customised (signal / slot anyone)
They don't make any sense outside the context of the *interface*
(b) I want to be able to scope short indentifier names for these specialised
events, rather than relying on loooong identifier names to guarantee
uniqueness

eg.

namespace ABC
{
public delegate CoffeeInfoHandler(object sender, CoffeeInfo info);

interface ICoffeeMachines
{
event CoffeeInfoHandler InfoHandlerList;
}
}

Why can't I just have

namespace ABC
{
interface ICoffeeMachines
{
public delegate InfoHandler(object sender, CoffeeInfo info);
event InfoHandler InfoHandlerList;
}
}

Then I can can create handlers for ICoffeeMachines.InfoHandler rather than
CoffeeInfoHandler.
Having the unscoped indentifier ends up generating unwieldly names to
guarantee uninoquness, which I do not like.
It also prevents using the use of local scoping, eg. an implementor of
ICoffeeMachines could locally scope ICoffeeMachines.InfoHandler as
InfoHandler.
this is a very attractive proposition since it doesn't force tight
(synthetic) identifier coupling.
==> An inappropriately structured interface can be easily restructured in
later design iterations.

Cheers,

-- Steve
 

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

Top