Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between events in VB and C#?

Tags:

c#

vb.net

events

I am a VB programmer working my way into C#. I learned how to create and raise events in vb and am finding that it isnt done the same way in C#. Has anybody come across an article that will help me to understand how to do events in C# and maybe explain why it is different in VB.

Thanks

like image 973
Mike Murphy Avatar asked Mar 30 '10 16:03

Mike Murphy


2 Answers

Does this help?

VB.NET vs. C# - Delegates / Events

like image 191
Kyle Rosendo Avatar answered Nov 15 '22 08:11

Kyle Rosendo


The main difference is the syntax that's used. Underneath, they use the exact same mechanisms within the CLR.

However, VB.NET provides special syntax via WithEvents and Handles, allowing you to do:

Dim WithEvents button1 As Button

Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyButton.Click 
    ' Handle button click
End Sub

C# doesn't provide an equivelent - all events must be explicitly subscribed to via event +=, which is more like VB.NET's AddHandler statement.

like image 23
Reed Copsey Avatar answered Nov 15 '22 07:11

Reed Copsey