Delegates and events, in the interface

G

Guest

Hello.

I need to implement the publish/subscribe architecture where class B
(server) does not have a reference to class A (which is in a client), but
class B still has to subscribe to the event raised by the class A.

Here's the problem: Class A implements the interface IClient that is on the
server, everything's fine. When the method Foo of the class A is executed,
the event is raised, passing SomeData as a reference. Now, in order to
subscribe to the event from the server, I need to do something like this:

Foo f = new Foo();
IClient icl = f as IClient();

and then:

icl.SomeEvent += new ... bla, bla...

but I DO NOT HAVE a reference to class A, method Foo, which is the whole
point from the very beginning. Is there another way to subscribe to the event
that is wrapped in the interface without having to know the class that's
implementing it on the client side?

Thanks.
 
P

Peter Rilling

No.

You can always bind to interfaces, but there must be an actual object to
work against, otherwise, how would any code get executed since interfaces
have no implementation.
 
G

Guest

It works, I already sorted it out. The class A would pass the reference of
itself to class B, which would then do something like:

public void ConnectToServer( IClient client )
{
client.someEvent += client.someEventHandler( methodThatImplementsIt );
}

when the client would do:

ConnectToServer( this );

in order to publish the event.

Just it was not that obvious how the server would subscribe to the event
using the client object from the interface, but there's the solution.

Cheers.
 

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