Hi,
I'm still trying to wrap my own mind around the delegate thing, but here is a more simpler example than what's in the book.
using System;
namespace Timer
{
class Clock
{
delegate void Tick(int hrs, int min, int sec);
public void RefreshTime(int hrs, int min, int sec)
{
Console.WriteLine("The new time is: {0}:{1}:{2}", hrs, min, sec);
}
[STAThread]
static void Main(string[] args)
{
Clock wall = new Clock();
Tick m = new Tick(wall.RefreshTime);
m(12,29,59);
}
}
}
We have a class of Clock with a method that writes the hours, min, and seconds to the screen called RefreshTime(). Our delegate Tick also has the same param so we can now use it to call this method. In the Main, we create a Clock object. This will allow us access to the RefreshTime method. We can now create an Tick type object called "m". Now instead of writing long winded paths back, m now can take on param that matched Tick, which also match RefreshTime(). m now resolves to wall.RefreshTime(12,29,59).
Hope this helps
Ray Bohnenstiehl
|