Including Delegates in Interfaces

R

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();

}
 
M

Mattias Sjögren

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
 
R

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);

}

}
 
P

Peter Duniho

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
 
S

Samuel R. Neff

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
 
R

RSH

Oh I get it! that makes sense. I was a bit foggy on the delegate, but that
sounds like a plan.

Thanks!
Ron
 
J

Jon Skeet [C# MVP]

RSH said:
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.
 

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