Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

[VB.net / C#] Events/Delegates example [SOURCE]

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!
That looks very confusing, can you comment a little please? Rated 5 stars :)
(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 :(
(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 ;).
I get the delegate's but I don't get how you make your own event handler :(
(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 ;).
A little buump! 😁
this is hard to learn but harder to explain it :P I can make them but I can't explain how and what lol
(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.
A little bump for my post :D!
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.
(Aug 7, 2010 12:14 PM)laptopguy 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 <.<)
Bumppp! I'll do another example soon :).
very nice i couldnt have explained it better
(Aug 11, 2010 11:02 AM)killerdoob420 Wrote:  very nice i couldnt have explained it better

Thanks for the comment 😁!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by darkdoctorJun 7, 2018 12:10 PM 1 162 Jun 7, 2018 12:10 PM
Last Post: Washi
[C#] by gigajewFeb 26, 2018 01:28 PM 12 1,183 Feb 26, 2018 01:28 PM
Last Post: Menalix
[C#] by gigajewFeb 3, 2018 07:31 PM 0 296 Feb 3, 2018 07:31 PM
Last Post: gigajew