473,387 Members | 1,481 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,387 software developers and data experts.

Interfaces and Events

Hey everyone, I need some advice concerning Interfaces and delegates.

I would like to define a delegate inside of an interface (I know a
delegate is a class but hear me out)
Here is a sample:

Interface A

Class B inherits Interface A

Program C creates an instance of Class B and needs to consume events
from Class B.

I would like for Class B to be required to implement the delegates via
Interface A. Is this possible? Is this proper? Can/Should I use an
abstract class for this? Is there a better way?

Feb 21 '06 #1
13 1761
Sounds like a pipeline. Instead of using delegates and events, I would use
message passing. Each class will expose a bounded queue (blocking style)
and have a worker thread (or a user TP) block on messages and handle them as
needed.

--
William Stacey [MVP]

<go********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
| Hey everyone, I need some advice concerning Interfaces and delegates.
|
| I would like to define a delegate inside of an interface (I know a
| delegate is a class but hear me out)
|
|
| Here is a sample:
|
| Interface A
|
| Class B inherits Interface A
|
| Program C creates an instance of Class B and needs to consume events
| from Class B.
|
| I would like for Class B to be required to implement the delegates via
| Interface A. Is this possible? Is this proper? Can/Should I use an
| abstract class for this? Is there a better way?
|
Feb 21 '06 #2
William,

Thank you for that. It's a shame I have no idea how to implement what you
are speaking of. Can you point me (and others reading this) in the right
direction?

Regards..
"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:Od****************@TK2MSFTNGP15.phx.gbl...
Sounds like a pipeline. Instead of using delegates and events, I would
use
message passing. Each class will expose a bounded queue (blocking style)
and have a worker thread (or a user TP) block on messages and handle them
as
needed.

--
William Stacey [MVP]

<go********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
| Hey everyone, I need some advice concerning Interfaces and delegates.
|
| I would like to define a delegate inside of an interface (I know a
| delegate is a class but hear me out)
|
|
| Here is a sample:
|
| Interface A
|
| Class B inherits Interface A
|
| Program C creates an instance of Class B and needs to consume events
| from Class B.
|
| I would like for Class B to be required to implement the delegates via
| Interface A. Is this possible? Is this proper? Can/Should I use an
| abstract class for this? Is there a better way?
|

Feb 21 '06 #3
<go********@gmail.com> wrote:
Hey everyone, I need some advice concerning Interfaces and delegates.

I would like to define a delegate inside of an interface (I know a
delegate is a class but hear me out)
Here is a sample:

Interface A

Class B inherits Interface A

Program C creates an instance of Class B and needs to consume events
from Class B.

I would like for Class B to be required to implement the delegates via
Interface A. Is this possible? Is this proper? Can/Should I use an
abstract class for this? Is there a better way?


You can certainly include events (not delegates) within interfaces.
It's not terribly common, but it makes sense in certain situations.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 21 '06 #4
Jon,

That's how I originally set the interface up. However, I found that I could
not figure out how Program C was supposed to cosume those events. The events
are raised by Class B and need to be consumed by Program C.

My understanding is that Class B need to implement the delegates and Program
C should consume those events. How can I make this situation work?

Regards

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<go********@gmail.com> wrote:
Hey everyone, I need some advice concerning Interfaces and delegates.

I would like to define a delegate inside of an interface (I know a
delegate is a class but hear me out)
Here is a sample:

Interface A

Class B inherits Interface A

Program C creates an instance of Class B and needs to consume events
from Class B.

I would like for Class B to be required to implement the delegates via
Interface A. Is this possible? Is this proper? Can/Should I use an
abstract class for this? Is there a better way?


You can certainly include events (not delegates) within interfaces.
It's not terribly common, but it makes sense in certain situations.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Feb 21 '06 #5
If this will be a one-off and one or two classes, I don't want to send you
off on into a mess. But if you will be doing a lot of this in your app, it
is a pattern that can bear a lot of fruit and simplify things. For some
background, check out
http://www.eecs.harvard.edu/~mdw/papers/seda-sosp01.pdf
It is server focused, but the same idea is great for client side and UI
update processing as well. Some nice things are: message based, not method
based so it is easy to refactor code and is loosely coupled. It is
naturally pluggable for easy adding and changing Stages, even at runtime.
Each stage can send messages back to itself or any other stage. Shutdown
processing is as easy as sending a stop message which gets sent along your
Stages (this can sometimes be one of the harder issues to handle cleanly
with other patterns). You can throttle and manage speed at each stage as
you have the queue as the focus point. Many others. First, you will need a
bounded-blocking thread safe queue. Here is mine that also have some
examples using pipes and queues and thread pools.
http://channel9.msdn.com/ShowPost.aspx?PostID=161030 I am actually working
on a server using this "SEDA" pattern and seems to make a lot of sense so
far and seems to be clean. I will try to work up something simple and put
in the C9 Sandbox.

--
William Stacey [MVP]

"Bob Jones" <dd******@eskyesolutions.com> wrote in message
news:uL****************@TK2MSFTNGP15.phx.gbl...
| William,
|
| Thank you for that. It's a shame I have no idea how to implement what you
| are speaking of. Can you point me (and others reading this) in the right
| direction?
|
| Regards..
|
|
| "William Stacey [MVP]" <wi************@gmail.com> wrote in message
| news:Od****************@TK2MSFTNGP15.phx.gbl...
| > Sounds like a pipeline. Instead of using delegates and events, I would
| > use
| > message passing. Each class will expose a bounded queue (blocking
style)
| > and have a worker thread (or a user TP) block on messages and handle
them
| > as
| > needed.
| >
| > --
| > William Stacey [MVP]
| >
| > <go********@gmail.com> wrote in message
| > news:11**********************@f14g2000cwb.googlegr oups.com...
| > | Hey everyone, I need some advice concerning Interfaces and delegates.
| > |
| > | I would like to define a delegate inside of an interface (I know a
| > | delegate is a class but hear me out)
| > |
| > |
| > | Here is a sample:
| > |
| > | Interface A
| > |
| > | Class B inherits Interface A
| > |
| > | Program C creates an instance of Class B and needs to consume events
| > | from Class B.
| > |
| > | I would like for Class B to be required to implement the delegates via
| > | Interface A. Is this possible? Is this proper? Can/Should I use an
| > | abstract class for this? Is there a better way?
| > |
| >
| >
|
|
Feb 21 '06 #6

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<go********@gmail.com> wrote:

You can certainly include events (not delegates) within interfaces.
It's not terribly common, but it makes sense in certain situations.


Really? It's not common? I do it all the time. Is there some reason it's not
common?

To give you an example, for most of my larger apps, I have generally create
some sort of plugin framework (I'm still trying to come up with a good,
reusable plugin framework, but can't seem to accomplish this). Normally,
I'll create interfaces which are implemented in the main app, usually
something like an IApplication interface. Then I'll have events that get
triggered for things such as initialization completing, app shutting down,
windows opening or closing, or whatever.

This is simply an example. I could think of tons of others. I could
implement these as methods in the plugins and have the main app call those
methods when these events take place, but then every plugin would have to
implement the methods.

Anyway, just curious if there's something wrong with having events in
interfaces or you just don't see it very commonly.

Pete
Feb 21 '06 #7
Bob Jones wrote:
Jon,

That's how I originally set the interface up. However, I found that I could
not figure out how Program C was supposed to cosume those events. The events
are raised by Class B and need to be consumed by Program C.

My understanding is that Class B need to implement the delegates and Program
C should consume those events. How can I make this situation work?

Regards


<snip />

Not sure if this is what you are looking for:

namespace ConsoleApplication14
{
delegate void FooHandler();

interface IXox
{
event FooHandler Foo;
}

class Lulli : IXox
{
public event FooHandler Foo;

public void OnFoo()
{
if (Foo != null)
Foo();
}
}

class Program
{
static void Main(string[] args)
{
IXox xox = new Lulli();
xox.Foo += new FooHandler(xox_Foo);

// Demo: Raise the event!
((Lulli)xox).OnFoo();
}

static void xox_Foo()
{
Console.WriteLine("Foo called.");
}
}
}

Or do you mean the C is really another different programm? Then you have
to do some sort of IPC, e.g. remoting.

HTH,
Andy

--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40@*NO*SPAM*gmx.net
Feb 21 '06 #8
It might look something like this:
namespace MyInterface
{
public delegate void MyEventHandler(object sender, MyEventArgs e);

interface InterfaceA
{
....

event MyEventHandler MyEvent;

}
}
namespace MyImplementation
{
using MyInterface;

public class ClassB : InterfaceA
{

...

protected virtual OnMyEvent(MyEventArgs e)
{
if (MyEvent != null)
{
MyEvent(this, e);
}
}
...

public event MyEventHandler MyEvent;
}
}

Calling OnMyEvent triggers MyEvent.

Then your program could have something like this somewhere:

public void AMethod()
{
ClassB myClassBOb = new ClassB();
myClassBOb.MyEvent += new MyEventHandler(ClassBMyEvent)
}

private void ClassBMyEvent(object sender, MyEventArgs e)
{
[do something here]
}
Hope that makes sense.

Pete
"Bob Jones" <dd******@eskyesolutions.com> wrote in message
news:u0***************@tk2msftngp13.phx.gbl...
Jon,

That's how I originally set the interface up. However, I found that I
could not figure out how Program C was supposed to cosume those events.
The events are raised by Class B and need to be consumed by Program C.

My understanding is that Class B need to implement the delegates and
Program C should consume those events. How can I make this situation work?

Regards

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<go********@gmail.com> wrote:
Hey everyone, I need some advice concerning Interfaces and delegates.

I would like to define a delegate inside of an interface (I know a
delegate is a class but hear me out)
Here is a sample:

Interface A

Class B inherits Interface A

Program C creates an instance of Class B and needs to consume events
from Class B.

I would like for Class B to be required to implement the delegates via
Interface A. Is this possible? Is this proper? Can/Should I use an
abstract class for this? Is there a better way?


You can certainly include events (not delegates) within interfaces.
It's not terribly common, but it makes sense in certain situations.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Feb 21 '06 #9
Andy,

Almost! The only difference between your code and what I am trying to
accomplish is that I want the program to receive (consume) the event, not
fire the event. The event should get fired in class Lulli.

Regards

"Andy" <me@privacy.net> wrote in message
news:46************@individual.net...
Bob Jones wrote:
Jon,

That's how I originally set the interface up. However, I found that I
could not figure out how Program C was supposed to cosume those events.
The events are raised by Class B and need to be consumed by Program C.

My understanding is that Class B need to implement the delegates and
Program C should consume those events. How can I make this situation
work?

Regards


<snip />

Not sure if this is what you are looking for:

namespace ConsoleApplication14
{
delegate void FooHandler();

interface IXox
{
event FooHandler Foo;
}

class Lulli : IXox
{
public event FooHandler Foo;

public void OnFoo()
{
if (Foo != null)
Foo();
}
}

class Program
{
static void Main(string[] args)
{
IXox xox = new Lulli();
xox.Foo += new FooHandler(xox_Foo);

// Demo: Raise the event!
((Lulli)xox).OnFoo();
}

static void xox_Foo()
{
Console.WriteLine("Foo called.");
}
}
}

Or do you mean the C is really another different programm? Then you have
to do some sort of IPC, e.g. remoting.

HTH,
Andy

--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40@*NO*SPAM*gmx.net

Feb 21 '06 #10
That SEDA pattern I posted above seems like it would make for a nice plugin
pattern as well. Think all the plugin needs is the expected Message type.
Think either interface or abstract class would work, but have not run that
around yet.

--
William Stacey [MVP]

"Pete Davis" <pdavis68@[nospam]hotmail.com> wrote in message
news:28******************************@giganews.com ...
|
| "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
| news:MP************************@msnews.microsoft.c om...
| > <go********@gmail.com> wrote:
| >
| > You can certainly include events (not delegates) within interfaces.
| > It's not terribly common, but it makes sense in certain situations.
| >
|
| Really? It's not common? I do it all the time. Is there some reason it's
not
| common?
|
| To give you an example, for most of my larger apps, I have generally
create
| some sort of plugin framework (I'm still trying to come up with a good,
| reusable plugin framework, but can't seem to accomplish this). Normally,
| I'll create interfaces which are implemented in the main app, usually
| something like an IApplication interface. Then I'll have events that get
| triggered for things such as initialization completing, app shutting down,
| windows opening or closing, or whatever.
|
| This is simply an example. I could think of tons of others. I could
| implement these as methods in the plugins and have the main app call those
| methods when these events take place, but then every plugin would have to
| implement the methods.
|
| Anyway, just curious if there's something wrong with having events in
| interfaces or you just don't see it very commonly.
|
| Pete
|
|
Feb 21 '06 #11
The event IS being fired in class Lulli. It's getting fired in the OnFoo()
method where if (Foo != null) Foo();

Foo is the event. the statement 'Foo();' triggers it.

Pete

"Bob Jones" <dd******@eskyesolutions.com> wrote in message
news:Ob**************@TK2MSFTNGP10.phx.gbl...
Andy,

Almost! The only difference between your code and what I am trying to
accomplish is that I want the program to receive (consume) the event, not
fire the event. The event should get fired in class Lulli.

Regards

"Andy" <me@privacy.net> wrote in message
news:46************@individual.net...
Bob Jones wrote:
Jon,

That's how I originally set the interface up. However, I found that I
could not figure out how Program C was supposed to cosume those events.
The events are raised by Class B and need to be consumed by Program C.

My understanding is that Class B need to implement the delegates and
Program C should consume those events. How can I make this situation
work?

Regards


<snip />

Not sure if this is what you are looking for:

namespace ConsoleApplication14
{
delegate void FooHandler();

interface IXox
{
event FooHandler Foo;
}

class Lulli : IXox
{
public event FooHandler Foo;

public void OnFoo()
{
if (Foo != null)
Foo();
}
}

class Program
{
static void Main(string[] args)
{
IXox xox = new Lulli();
xox.Foo += new FooHandler(xox_Foo);

// Demo: Raise the event!
((Lulli)xox).OnFoo();
}

static void xox_Foo()
{
Console.WriteLine("Foo called.");
}
}
}

Or do you mean the C is really another different programm? Then you have
to do some sort of IPC, e.g. remoting.

HTH,
Andy

--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40@*NO*SPAM*gmx.net


Feb 21 '06 #12
Bob Jones <dd******@eskyesolutions.com> wrote:
That's how I originally set the interface up. However, I found that I could
not figure out how Program C was supposed to cosume those events. The events
are raised by Class B and need to be consumed by Program C.

My understanding is that Class B need to implement the delegates and Program
C should consume those events. How can I make this situation work?


Other way round - if program C is meant to *consume* those events, then
it needs to subscribe to the events with event handlers which implement
the delegates.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 21 '06 #13
Pete Davis <pdavis68@[nospam]hotmail.com> wrote:
You can certainly include events (not delegates) within interfaces.
It's not terribly common, but it makes sense in certain situations.
Really? It's not common? I do it all the time. Is there some reason it's not
common?


Not particularly - it's just not something I see that often.
To give you an example, for most of my larger apps, I have generally create
some sort of plugin framework (I'm still trying to come up with a good,
reusable plugin framework, but can't seem to accomplish this). Normally,
I'll create interfaces which are implemented in the main app, usually
something like an IApplication interface. Then I'll have events that get
triggered for things such as initialization completing, app shutting down,
windows opening or closing, or whatever.


Those all seem like perfectly good examples. There's nothing wrong with
them at all - it's just not that common. For instance, I don't think
there are very many interfaces in the framework that specify events.

(Of course, now I'll find there are hundreds...)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 21 '06 #14

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

Similar topics

21
by: Franco Gustavo | last post by:
Hi, Please help me to understand this, because I don't see what I'm missing. I was reading a lot of examples on Internet that explain that C# doesn't implement multiple inheritance it...
3
by: Darren | last post by:
Do event members belong in interfaces? I have used events in interfaces to report state changes and they work well. However; I really never see event members in any of the framework interfaces. ...
2
by: raffelm | last post by:
Bear with my while I try to explain... I've created these two interfaces: public interface ILoggingEvents { event LoggingWindowClosing OnClosing;} public interface ILogging { ... } I then...
2
by: Wiktor Zychla [C# MVP] | last post by:
Hi, suppose there are two interfaces that contain methods with the same name but different signature: interface I1 { void F(); } interface I2 { int F(); } it is then easy to implement...
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...
3
by: steve | last post by:
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...
22
by: RSH | last post by:
Hi, I have been reading on interfaces working on samples I've run across on the web. For the life of me I cannot seem to grasp them. It appears to me that interfaces are simply blueprints to...
2
by: =?Utf-8?B?TkVXMi5ORVQ=?= | last post by:
Two questions: 1) I have an assembly that defines the interface for a component. The assembly only contains the interface (as per component development guidance), a different assembly contains...
5
by: raylopez99 | last post by:
I understand delegates (static and non-static) and I agree they are very useful, and that the "Forms" used in the Windows .NET API could not work without them. That said, I'm curious as to how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.