Simple C# Question? How to pass an event delegate as a parameter?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Ok, I have a class called 'publisher' which exposes a delegate event as
follows:
public event subscriberDelegate subscribers;

The definition of subscriberDelegate being:
public delegate void subscriberDelegate(uint nValue);

I also have a 'subscriber' class which exposes a function that conforms to
the subscriberDelegate as follows:

public void Insert(uint nValue)

I've connected all this up and it's working fine. However, I'm now
attempting to write a function which can be passed the publishers event and
the subscribers function and then connects them together.

My initial idea was as follows:

void Connect(event subscriberDelegate evt, subscriberDelegate delegate)
{
evt+=delegate;
}

But I get a compiler error on the first parameter stating that a type is
required. Anyone got any suggestions how I can accomplish this?
 
<"=?Utf-8?B?SmFzb24gR2lsYmVydA==?=" <Jason
Ok, I have a class called 'publisher' which exposes a delegate event as
follows:
public event subscriberDelegate subscribers;

The definition of subscriberDelegate being:
public delegate void subscriberDelegate(uint nValue);

I also have a 'subscriber' class which exposes a function that conforms to
the subscriberDelegate as follows:

public void Insert(uint nValue)

I've connected all this up and it's working fine. However, I'm now
attempting to write a function which can be passed the publishers event and
the subscribers function and then connects them together.

My initial idea was as follows:

void Connect(event subscriberDelegate evt, subscriberDelegate delegate)
{
evt+=delegate;
}

But I get a compiler error on the first parameter stating that a type is
required. Anyone got any suggestions how I can accomplish this?

Rather than defining an *event*, you need to define a *delegate* - you
can then pass that to a method.
 
Jon / All,

Not sure if I clearly stated my problem. What I want to be able to do is
pass a member variable to a function, where the member variable is:

public event subscriberDelegate subscribers;

Jon, I understand your reply regarding writing a delegate, which I have done
and which works, but this is basically a workaround. I really need to be able
to pass an event as a parameter to a function. Any chance someone could post
an example of how to achieve this.
 
Jason Gilbert said:
Not sure if I clearly stated my problem. What I want to be able to do is
pass a member variable to a function, where the member variable is:

public event subscriberDelegate subscribers;

Jon, I understand your reply regarding writing a delegate, which I have done
and which works, but this is basically a workaround. I really need to be able
to pass an event as a parameter to a function. Any chance someone could post
an example of how to achieve this.

You can't do that. An event is actually just a set of methods, just
like a property. You *could* pass an EventInfo if you really wanted,
but that would be fairly ugly.
 
Back
Top