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

Including Delegates in Interfaces

RSH
I am declaring an interface and I want to make sure any classes that
implement it implement a delegate. But I am getting a compliler error:
Error 1 'UpdateHandler': interfaces cannot declare types

How do I correctly include a delegate in an interface?

Thanks,
Ron
public interface ICommunicator

{

delegate void UpdateHandler(object sender); <<<< Complier Error

event UpdateHandler UpdateEvent;

string Name{get;}

void Communicate();

void Notify();

}
May 18 '07 #1
6 8495
>I am declaring an interface and I want to make sure any classes that
implement it implement a delegate.
What exactly do you mean by "implement a delegate"?

If you want to force the implementing class to have a method that
matches the UpdateHandler delegate signature, include such a method in
the interface (but declare the delegate outside of it).

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 18 '07 #2
RSH
My bad.

I would like to force the implementing class to implement the delegate and
event.

Thanks,
Ron
public interface ICommunicator

{

delegate void UpdateHandler(object sender); <<-- Complie Error

event UpdateHandler UpdateEvent; <<-- Complie Error

string Name{get;}

void Communicate();

void Notify();

}

public class Communicator1 : ICommunicator

{

public delegate void UpdateHandler(object sender);

public event UpdateHandler UpdateEvent;

private string m_name;

public Communicator1(string Name)

{

m_name = Name;

}

public string Name

{

get

{

return m_name;

}

}

public void Communicate()

{

Notify();

}

public void Notify()

{

if (UpdateEvent != null) UpdateEvent(this);

}

}
May 18 '07 #3
On Fri, 18 May 2007 10:29:34 -0700, RSH <wa*************@yahoo.comwrote:
I would like to force the implementing class to implement the delegate
and event.
Well, the delegate is a type declaration. It doesn't make sense for an
interface to define types, because an interface is essentially a contract
requiring *other* types to implement certain things. Since types aren't
something that are implemented, they aren't the sort of thing that would
go into an interface.

So, what you really want is to define the delegate type somewhere else (in
the same namespace with the interface, for example), and then include the
event in the interface (once the delegate has been propertly declared, you
shouldn't have any trouble with the event in the interface).

Pete
May 18 '07 #4

Leave the event declaration as is but put the delegate declation
outside the interface. That'll get you what you want--implementers
that have an event of the prescribed delegate.

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Fri, 18 May 2007 13:29:34 -0400, "RSH" <wa*************@yahoo.com>
wrote:
>My bad.

I would like to force the implementing class to implement the delegate and
event.

Thanks,
Ron
public interface ICommunicator

{

delegate void UpdateHandler(object sender); <<-- Complie Error

event UpdateHandler UpdateEvent; <<-- Complie Error

string Name{get;}

void Communicate();

void Notify();

}
May 18 '07 #5
RSH
Oh I get it! that makes sense. I was a bit foggy on the delegate, but that
sounds like a plan.

Thanks!
Ron

"Samuel R. Neff" <sa********@nomail.comwrote in message
news:qs********************************@4ax.com...
>
Leave the event declaration as is but put the delegate declation
outside the interface. That'll get you what you want--implementers
that have an event of the prescribed delegate.

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Fri, 18 May 2007 13:29:34 -0400, "RSH" <wa*************@yahoo.com>
wrote:
>>My bad.

I would like to force the implementing class to implement the delegate and
event.

Thanks,
Ron
public interface ICommunicator

{

delegate void UpdateHandler(object sender); <<-- Complie Error

event UpdateHandler UpdateEvent; <<-- Complie Error

string Name{get;}

void Communicate();

void Notify();

}

May 18 '07 #6
RSH <wa*************@yahoo.comwrote:
Oh I get it! that makes sense. I was a bit foggy on the delegate, but that
sounds like a plan.
The trouble is that the word "delegate" is used for both the type and
instances of it. I tend to explicitly say "delegate type" or "delegate
instance" these days to make sure it's clear.

--
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
May 18 '07 #7

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

Similar topics

3
by: Sam | last post by:
I’m just starting to learn delegates. I’m at the very beginning. If I understand correctly, delegates are for when you want to pass a function as a parameter. For example the client provides 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...
4
by: GTi | last post by:
I have this solution: -> -> A knows about B but not C C knows about B but not A Type A can be a EXE file or NT Service There can be many of type A loaded but not necessarily the same...
3
by: Rich | last post by:
Hello, I recently started using delegates in my VB apps to handle asynchronous operations. Based on the examples that I followed, delegates appear to work in a similar way as Interfaces. ...
2
by: kristian.freed | last post by:
Hi, I currently work in a project written fully in C# where we make extensive use of delegates and events. We have a model where a "state", an object holding data but not much code but which...
6
by: =?Utf-8?B?Sko=?= | last post by:
I have a logger component that logs to multiple sources, ie textfile, eventlog etc. and I have two methods that depending on where I call up my logger comp. one of them will be called. For ex. if...
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...
2
by: Berryl Hesh | last post by:
I'm wondering if a delegate will help me solve how to reuse the unit test fixture below (I only show one test of several here). The parts that I want to vary in it are the IClosingWindowView &...
4
by: samadams_2006 | last post by:
Hello, I'm trying to figure out why delegates are such a great thing. For example, I came across the following line of code: "This ability to refer to a method as a parameter makes delegates...
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: 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
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
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...
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
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,...
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
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...

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.