473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C# - Adding event delegate declarations to interfaces

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
}
}
}

Jul 3 '06 #1
3 11535
steve <st***@discussions.microsoft.comwrote:
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

--
http://barrkel.blogspot.com/
Jul 3 '06 #2
steve wrote:
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?

--
Hope this helps,
Tom Spink
Jul 3 '06 #3
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
Jul 4 '06 #4

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

Similar topics

0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
5
by: Ian Richardson | last post by:
I'm writing some code which does one thing when onreadystatechange occurs, e.g. handle.onreadystatechange = function() { blah(handle,other_params) }; ....but sometimes I need to add another,...
2
by: Derrick | last post by:
How does one declare an event within an interface, so that every class which implements that interface must implement that event? I think I just need to specifiy the actual event as I would a...
3
by: Chua Wen Ching | last post by:
Hi there, I just read Chris Sells's article at http://www.codeproject.com/csharp/delegate_bedtime.asp?df=100&forumid=2983&select=922269#xx922269xx I wonder i can do this: 1) I want to...
6
by: Charles Law | last post by:
I have a class, which implements an interface. Let's say, that the interface looks something like Public Interface IEventSinks Sub ValueChanged(sender As Object, e As ValueChangedEventArgs) Sub...
5
by: Charles Law | last post by:
I think I asked the wrong question last time, so I am starting a separate post to distinguish them. Take five classes: ClassA and ClassW...Z ClassA raises five events: Event1...5 ClassW...Z...
4
by: Steven Cummings | last post by:
Hello, I'm currently developing on a large project where several assemblies build into DLLs that support the final .NET EXE. The original plan was to implement the project in VB.NET, so the EXE...
12
by: Benny Raymond | last post by:
I understand that you would normally want to raise an event on the same thread so that your code blocks until the event has been dealt with... however i've run into a situation where I have a...
4
by: tshad | last post by:
I am just getting started with events and had a couple of questions on why they do what they do. If you have a textbox and you want to handle an event you can just do: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
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...
0
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,...

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.