Hack Forums
[C#] [VB.net / C#] Events/Delegates example [SOURCE] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] [VB.net / C#] Events/Delegates example [SOURCE] (/showthread.php?tid=575989)



[VB.net / C#] Events/Delegates example [SOURCE] - Charny - Jul 29, 2010

Hello, I made a little events/delegates example. I did it because I was bored and I decided to make it :P. I'm sharing it with you because events and delegates can be hard when your learn them and this example could help you.

I know, there's better way to divide, but I needed a subject for the example. So I took the division.

C#
Spoiler (Click to View)

Visual Basic .NET
Spoiler (Click to View)

Enjoy! If you have any questions, ask them here!


* - CometJack - Jul 29, 2010

That looks very confusing, can you comment a little please? Rated 5 stars :)


* - xhtmlphp™ - Jul 29, 2010

(Jul 29, 2010 08:01 PM)CometJack Wrote:  That looks very confusing, can you comment a little please? Rated 5 stars :)

Ya, I think Charny should do an explanation on this :P

I don't understand Delegates, I've seen them being mentioned a few times but never understood the use of them :(


* - Charny - Jul 29, 2010

(Jul 29, 2010 08:26 PM)xhtmlphp Wrote:  Ya, I think Charny should do an explanation on this :P

I don't understand Delegates, I've seen them being mentioned a few times but never understood the use of them :(

You can use delegates like that.. :

Code
public delegate void Write(string str);

  private void writeString(string str)
  {
    MessageBox.Show(str);
  }

  private void writeLength(string str)
  {
    MessageBox.Show(str.Length.ToString());
  }

  private void TestDelegate()
  {
    Write write = new Write(writeString);
    write += new Write(writeLength);

    write("test");
  }

Try this code and check the result, then you can play with the code ;).


* - CometJack - Jul 29, 2010

I get the delegate's but I don't get how you make your own event handler :(


* - Charny - Jul 29, 2010

(Jul 29, 2010 08:41 PM)CometJack Wrote:  I get the delegate's but I don't get how you make your own event handler :(

Read some events tutorials, you will learn it ;).


* - Charny - Jul 30, 2010

A little buump! 😁


* - enacje - Jul 30, 2010

this is hard to learn but harder to explain it :P I can make them but I can't explain how and what lol


* - Charny - Jul 30, 2010

(Jul 30, 2010 08:40 AM)enacje Wrote:  this is hard to learn but harder to explain it :P I can make them but I can't explain how and what lol

Haha, same for me 😝. That's why I didn't really gave informations on them.


* - Charny - Aug 6, 2010

A little bump for my post :D!


* - laptopguy - Aug 7, 2010

Nice one Charny. People who call themselves programmers and don't understand this type of shit aren't tbh. And why was the thread allowed to die ? This is one of those things in this section that is actually useful, like the things Vorfin and Deathader post sometimes.


* - Charny - Aug 7, 2010

(Aug 7, 2010 12:14 PM)kRaeTwin Wrote:  Nice one Charny. People who call themselves programmers and don't understand this type of shit aren't tbh. And why was the thread allowed to die ? This is one of those things in this section that is actually useful, like the things Vorfin and Deathader post sometimes.

Thanks, and why is this thread dieing? Because most 'coders' here doesn't bother learning delegates & events because their IDE does everything for them..

(Sorry if it didn't make sense <.<)


* - Charny - Aug 11, 2010

Bumppp! I'll do another example soon :).


* - killerdoob420 - Aug 11, 2010

very nice i couldnt have explained it better


* - Charny - Aug 11, 2010

(Aug 11, 2010 11:02 AM)killerdoob420 Wrote:  very nice i couldnt have explained it better

Thanks for the comment 😁!