andrewducker: (Default)
[personal profile] andrewducker
I'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

Date: 2005-02-25 08:59 pm (UTC)
From: [identity profile] aberbotimue.livejournal.com
you sit in this industry..

you have managed in this industry,
yet you seem to post, all happy, that you are coding new shit...

why don't you want to move on ? this is far from a dig, I will I could just do somthing and be hapopy with it...

every management book you find, say that intelegent people get so bored, so quickly.. why you have to be diffent??

Date: 2005-02-26 12:51 am (UTC)
darkoshi: (Default)
From: [personal profile] darkoshi
Why did you name the method "OnStringEvent()"? Usually that would be the name of the method called to handle the event, not the method which initiates the event. Or maybe the name "StringEvent" is just misleading me... But I don't remember ever using delegates, or even the purpose of them, so I can't comment on whether your logic could be improved or not.

Oh, and if you want a GUI java development environment, since I think you had mentioned you didn't have one, check out Eclipse (www.eclipse.org). It's a free open-source IDE with a built-in JDE. Seems rather complicated to me so far, but we're planning on using it at my work, so I've been trying to get the hang of it.

Date: 2005-02-26 02:06 am (UTC)
moniqueleigh: Me after my latest haircut. Pic by <lj site="livejournal.com" user="seabat"> (c) 03/2008 (Default)
From: [personal profile] moniqueleigh
Really? There's proof of this? And people say I'm just too fickle to be bored after "only" 3 1/2 years at the same job. Hrmph. But yes -- I'm boooooooooooooooored there.

September 2025

S M T W T F S
  12 3 4 5 6
7 8 9 10 11 12 13
14151617181920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 13th, 2025 03:15 pm
Powered by Dreamwidth Studios