Feb. 25th, 2005
How I spent my Friday afternoon at work
Feb. 25th, 2005 06:53 pmI've just been moved to a new bit of the team, where I will be working in C# - despite the fact I've never been trained in it. I have, luckily, done some at home, in my own time, but bits of it have been known to make my head hurt. In attempt to refresh my memory I threw myself into the one thing that I never really got the hang of - events and delegates.
Delegates replace function pointers, and make them strongly typed (so rather than passing in a function pointer to a callback you pass in a delegate, which contains the function pointer and the type information about that function).
So in order to raise an event that passes a string from the event raiser to the event subscriber I've had to:
1) Declare a Delegate with String parameter called StringDelegate
public delegate void StringDelegate (String MessageString);
2) Declare an Event of type StringDelegate
public event StringDelegate StringEvent;
3) Create a method which takes a String parameter
public void AndyTest( String MessageToPrint )
{
MessageBox.Show( MessageToPrint,"Another Message Box");
}
4) Subscribe the method to the event, using the delegate.
StringEvent += new StringDelegate(AndyTest);
5) Create a method which raises the event
protected virtual void OnStringEvent(String EventMessage)
{
StringEvent(EventMessage);
}
6) Create some code which actually calls the code-raising method
OnStringEvent("Andy");
All of which has made my head hurt a fair bit. On the plus side, that final line _did_ make a message box pop up with the word "Andy" in it, so it's not all bad.
I'm sure the last two bits can be combined in some way, but I left work at that point, it being 5:45
Delegates replace function pointers, and make them strongly typed (so rather than passing in a function pointer to a callback you pass in a delegate, which contains the function pointer and the type information about that function).
So in order to raise an event that passes a string from the event raiser to the event subscriber I've had to:
1) Declare a Delegate with String parameter called StringDelegate
public delegate void StringDelegate (String MessageString);
2) Declare an Event of type StringDelegate
public event StringDelegate StringEvent;
3) Create a method which takes a String parameter
public void AndyTest( String MessageToPrint )
{
MessageBox.Show( MessageToPrint,"Another Message Box");
}
4) Subscribe the method to the event, using the delegate.
StringEvent += new StringDelegate(AndyTest);
5) Create a method which raises the event
protected virtual void OnStringEvent(String EventMessage)
{
StringEvent(EventMessage);
}
6) Create some code which actually calls the code-raising method
OnStringEvent("Andy");
All of which has made my head hurt a fair bit. On the plus side, that final line _did_ make a message box pop up with the word "Andy" in it, so it's not all bad.
I'm sure the last two bits can be combined in some way, but I left work at that point, it being 5:45
Having discovered that Microsoft produce a version of C# that can be downloaded for free (express edition), it is currently installing so I can play with it over the weekend.
[Poll #444284]
[Poll #444284]