Control delegate in an Interface

P

pigeonrandle

Hi,
I have a couple of forms on my client application that use an
asyncronous tcp client to send and recieve data from a server
application.
On each of these forms, i have a Listbox which i would like to add
status information to, like,

"Connected to server"
"Details Sent"
"Details Received"

Is it possible to have my forms implement an interface that includes a
generic delegate such as

"void AddStatusString(String s)"

where i pass the message s from the tcp client to my form's listbox?

This is sending me a bit screwy because i cant get my head around
exactly what needs to be in the interface. I know it can't be "void
AddStatusString(String s)" because this is the function in my form that
is Invoked BY a delegate, if you get my meaning.

Many thanks in advance,
James Randle.
 
J

Jon Skeet [C# MVP]

pigeonrandle said:
Hi,
I have a couple of forms on my client application that use an
asyncronous tcp client to send and recieve data from a server
application.
On each of these forms, i have a Listbox which i would like to add
status information to, like,

"Connected to server"
"Details Sent"
"Details Received"

Is it possible to have my forms implement an interface that includes a
generic delegate such as

"void AddStatusString(String s)"

where i pass the message s from the tcp client to my form's listbox?

This is sending me a bit screwy because i cant get my head around
exactly what needs to be in the interface. I know it can't be "void
AddStatusString(String s)" because this is the function in my form that
is Invoked BY a delegate, if you get my meaning.

I'm not sure I see why a delegate is needed. If you implement the
interface, you just write a method AddStatusString which is implemented
by adding the given string to the listbox - why do you need delegates?
 
T

Thomas T. Veldhouse

pigeonrandle said:
Hi,
I have a couple of forms on my client application that use an
asyncronous tcp client to send and recieve data from a server
application.
On each of these forms, i have a Listbox which i would like to add
status information to, like,

"Connected to server"
"Details Sent"
"Details Received"

Is it possible to have my forms implement an interface that includes a
generic delegate such as

"void AddStatusString(String s)"

No interface needed.


public delegate void AddStatusStringHandler(String s);

public class TcpServer
{
public event AddStatusStringHandler AddStatusStringEvent;
}

public class ClientForm : Form
{
public ClientForm(TcpServer tcpServer)
{
// form initialization code skipped

tcpServer += new AddStatusStringHandler(AddStatusString);
}

private AddStatusString(String s)
{
// add string to list box
}
}
 
T

Thomas T. Veldhouse

Thomas T. Veldhouse said:
public delegate void AddStatusStringHandler(String s);

public class TcpServer
{
public event AddStatusStringHandler AddStatusStringEvent;
}

Granted ... the name of the delegate and event are horrendous in my example
:)
 

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