Click here to Skip to main content
16,007,443 members
Home / Discussions / C#
   

C#

 
Questionprocess.start Pin
Sam 200623-Nov-05 11:51
Sam 200623-Nov-05 11:51 
AnswerRe: process.start Pin
Curtis Schlak.23-Nov-05 12:03
Curtis Schlak.23-Nov-05 12:03 
QuestionDelegates and events Pin
mirano23-Nov-05 9:21
mirano23-Nov-05 9:21 
AnswerRe: Delegates and events Pin
Leslie Sanford23-Nov-05 11:46
Leslie Sanford23-Nov-05 11:46 
GeneralRe: Delegates and events Pin
mirano23-Nov-05 13:00
mirano23-Nov-05 13:00 
GeneralRe: Delegates and events Pin
Leslie Sanford23-Nov-05 13:23
Leslie Sanford23-Nov-05 13:23 
GeneralRe: Delegates and events Pin
mirano26-Nov-05 6:19
mirano26-Nov-05 6:19 
GeneralRe: Delegates and events Pin
Leslie Sanford26-Nov-05 7:08
Leslie Sanford26-Nov-05 7:08 
You can have a reference to Class A on the server side without knowing its type as long as it implements an interface the server knows about and the reference is passed as a reference to that interface type.

Let's look at a bit of code so that what I wrote above is more concrete.

In the server library, you have the IClient interface:

public interface IClient
{
    event EventHandler ClientChanged;
}


Also, we have the Server class:

public class Server
{
    public void Connect(IClient client)
    {
        client.ClientChanged += new EventHandler(HandleClientChangedEvent);
    }

    public void Disconnect(IClient client)
    {
        client.ClientChanged -= new EventHandler(HandleClientChangedEvent);
    }

    private void HandleClientChangedEvent(object sender, EventArgs e)
    {
        // Do something here in response to the client's event.
    }
}


Now, the Server can attach and detach itself to the client's event without having to know its type. All it knows is that the client implements the IClient interface.

Make sense?

On the client side, we do have to know about the server, but the dependency is one-side, i.e. not circular, so we're ok:

public class SomeClient
{
    public SomeClient(Server s)
    {
        s.Connect(this);
    }

    // ...
}


-- modified at 13:12 Saturday 26th November, 2005
GeneralRe: Delegates and events Pin
mirano27-Nov-05 9:07
mirano27-Nov-05 9:07 
QuestionSounds - what were they thinking - they forgot about sound!!!!! Pin
Giles23-Nov-05 9:03
Giles23-Nov-05 9:03 
AnswerRe: Sounds - what were they thinking - they forgot about sound!!!!! Pin
Leslie Sanford23-Nov-05 9:21
Leslie Sanford23-Nov-05 9:21 
GeneralRe: Sounds - what were they thinking - they forgot about sound!!!!! Pin
Giles23-Nov-05 9:48
Giles23-Nov-05 9:48 
GeneralRe: Sounds - what were they thinking - they forgot about sound!!!!! Pin
Leslie Sanford23-Nov-05 10:49
Leslie Sanford23-Nov-05 10:49 
GeneralRe: Sounds - what were they thinking - they forgot about sound!!!!! Pin
Giles23-Nov-05 11:43
Giles23-Nov-05 11:43 
QuestionPrinter driver Pin
hg270523-Nov-05 8:45
hg270523-Nov-05 8:45 
AnswerRe: Printer driver Pin
Giles23-Nov-05 9:08
Giles23-Nov-05 9:08 
Questionmoving the mouse Pin
Sam 200623-Nov-05 8:10
Sam 200623-Nov-05 8:10 
AnswerRe: moving the mouse Pin
User 665823-Nov-05 8:38
User 665823-Nov-05 8:38 
GeneralRe: moving the mouse Pin
Sam 200623-Nov-05 11:59
Sam 200623-Nov-05 11:59 
QuestionSource safe slow pulling files - NAnt Get Pin
glocklt423-Nov-05 7:11
glocklt423-Nov-05 7:11 
AnswerRe: Source safe slow pulling files - NAnt Get Pin
Daniel Turini23-Nov-05 7:44
Daniel Turini23-Nov-05 7:44 
GeneralRe: Source safe slow pulling files - NAnt Get Pin
glocklt423-Nov-05 7:49
glocklt423-Nov-05 7:49 
QuestionProblem with Paint Pin
naglbitur23-Nov-05 6:48
naglbitur23-Nov-05 6:48 
AnswerRe: Problem with Paint Pin
Stanciu Vlad23-Nov-05 6:57
Stanciu Vlad23-Nov-05 6:57 
QuestionListview drag drop and Ghost icon issue Pin
sameerhanda23-Nov-05 4:09
sameerhanda23-Nov-05 4:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.