Difference between events in c# and vb.net
-
Why can't we make an event in c# without a delegate as eventhandler, like you can in vb.net ?
Learning without thought is labor lost; thought without learning is perilous. (Confucius)
Events in C# and VB.NET work the same way. If you're refering to the WithEvents keyword, it's just syntactic sugar for having the manual stuff done for you.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
Why can't we make an event in c# without a delegate as eventhandler, like you can in vb.net ?
Learning without thought is labor lost; thought without learning is perilous. (Confucius)
If you don't want to bother creating your own custom delegate you have two other choices in c# 1. Use an inline delegate. 2. Use Generics eg.
EventHandler
In VB.Net you also have two options in addition to creating a custom delegate> 1. Use Generics 2. UseWithEvents
WithEvents
works rather differently as it gives you rather more than just syntactic sugar, it also deals with reattaching the event handlers to the new object if you change the object referenced. You can even define the event handlers before initialising the object that raises the events. In C# you have to do that yourself, and you have to have an instance before you can hook up events. So, C# or VB.NET you can roll your own. C# you get inline delegates VB.NET you get less code for complicated event handling scenarios. Personally I would rather have a little more code that shows me what is really going on. Then it is easier to realise I didn't need half of it in the first place!If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles] [My Website]
-
If you don't want to bother creating your own custom delegate you have two other choices in c# 1. Use an inline delegate. 2. Use Generics eg.
EventHandler
In VB.Net you also have two options in addition to creating a custom delegate> 1. Use Generics 2. UseWithEvents
WithEvents
works rather differently as it gives you rather more than just syntactic sugar, it also deals with reattaching the event handlers to the new object if you change the object referenced. You can even define the event handlers before initialising the object that raises the events. In C# you have to do that yourself, and you have to have an instance before you can hook up events. So, C# or VB.NET you can roll your own. C# you get inline delegates VB.NET you get less code for complicated event handling scenarios. Personally I would rather have a little more code that shows me what is really going on. Then it is easier to realise I didn't need half of it in the first place!If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles] [My Website]
-
Excellent reply. Again c# seems to be a lot clearer.
Learning without thought is labor lost; thought without learning is perilous. (Confucius)
Thanks. I use c# for my own projects, and VB.Net at work, so I get to see both sides of the coin, and struggle to get the two to do the same. I do prefer the flexibility and control in c#, but some of the RAD tools embedded in VB.Net are excellent. I wish it were possible to code classes in the same project in different languages so I could use the most appropriate one for the task at a class level. If that included managed C++ for some of the restricted stuff like Extended MAPI then even better.
If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850) [My Articles] [My Website]