T
tshad
Just trying to get my head around when to use delegates and when not to.
I understand delegates and use them with collection sorts and finds.
But in an example program I was playing with I am a little confused as to
why I would use delegates:
*****************************************************************
class Program
{
public delegate void MessagePrintDelegate(string msg);
protected delegate void LongRunningDelegate(MessagePrintDelegate
mCallBack);
static void Main(string[] args)
{
PrintMessage("This is a direct call to PrintMessage");
MessagePrintDelegate mpDel = new
MessagePrintDelegate(PrintMessage);
LongRunningDelegate lrd = new
LongRunningDelegate(LongRunningMethod);
mpDel("This is the direct Call to delegate");
lrd(mpDel);
LongRunningMethod(mpDel);
Console.Read();
}
static void LongRunningMethod(MessagePrintDelegate mpd)
{
for (int i = 0; i < 99; i++)
{
if (i % 10 == 0)
{
mpd(string.Format("Making progress. {0}% Complete.",
i));
}
}
}
static void PrintMessage(string msg)
{
Console.WriteLine("[{0}] {1}", DateTime.Now.ToShortTimeString(),
msg);
}
}
*****************************************************************
In the above code, doing the line:
mpDel("This is the direct Call to delegate");
wouldn't make any sense as I could just use a direct call to
PrintMessage() - which is all the delegate is doing.
In the line:
lrd(mpDel);
I am using a delegate to call a method to call a delegate that calls
PrintMessage.
This makes little sense as you can just call,
LongRunningMethod(mpDel);
which is a direct call to the method to call a delegate which call
PrintMessage.
In both cases, I using a delegate call when I don't need to.
What I am trying figure out is when I would need to use a delegate and when
I would not.
Thanks,
Tom
I understand delegates and use them with collection sorts and finds.
But in an example program I was playing with I am a little confused as to
why I would use delegates:
*****************************************************************
class Program
{
public delegate void MessagePrintDelegate(string msg);
protected delegate void LongRunningDelegate(MessagePrintDelegate
mCallBack);
static void Main(string[] args)
{
PrintMessage("This is a direct call to PrintMessage");
MessagePrintDelegate mpDel = new
MessagePrintDelegate(PrintMessage);
LongRunningDelegate lrd = new
LongRunningDelegate(LongRunningMethod);
mpDel("This is the direct Call to delegate");
lrd(mpDel);
LongRunningMethod(mpDel);
Console.Read();
}
static void LongRunningMethod(MessagePrintDelegate mpd)
{
for (int i = 0; i < 99; i++)
{
if (i % 10 == 0)
{
mpd(string.Format("Making progress. {0}% Complete.",
i));
}
}
}
static void PrintMessage(string msg)
{
Console.WriteLine("[{0}] {1}", DateTime.Now.ToShortTimeString(),
msg);
}
}
*****************************************************************
In the above code, doing the line:
mpDel("This is the direct Call to delegate");
wouldn't make any sense as I could just use a direct call to
PrintMessage() - which is all the delegate is doing.
In the line:
lrd(mpDel);
I am using a delegate to call a method to call a delegate that calls
PrintMessage.
This makes little sense as you can just call,
LongRunningMethod(mpDel);
which is a direct call to the method to call a delegate which call
PrintMessage.
In both cases, I using a delegate call when I don't need to.
What I am trying figure out is when I would need to use a delegate and when
I would not.
Thanks,
Tom