Public Events

C

Charlie Brown

What is the advantage of declaring events in #2 over #1. Which one
should be used?

#1
Public Event MyEvent(sender as object, e as eventargs)

Private Sub DoSomething()
RaiseEvent MyEvent(nothing, nothing)
End Sub

#2
Public Delegate Sub MyEventHandler(sender as object, e as eventargs)

Public Event MyEvent as MyEventHandler

Private Sub DoSomething()
RaiseEvent MyEvent(nothing, nothing)
End Sub
 
H

Herfried K. Wagner [MVP]

Charlie Brown said:
What is the advantage of declaring events in #2 over #1. Which one
should be used?

#1
Public Event MyEvent(sender as object, e as eventargs)

Private Sub DoSomething()
RaiseEvent MyEvent(nothing, nothing)
End Sub

#2
Public Delegate Sub MyEventHandler(sender as object, e as eventargs)

Public Event MyEvent as MyEventHandler

Private Sub DoSomething()
RaiseEvent MyEvent(nothing, nothing)
End Sub

#2 allows you to reuse the delegate type for more than one event.
 
G

Guest

#2 is more like C# does it. Either way is pretty much the same. If you use
#1, VB creates an Delegate under the covers. I like #1... less coding on my
part :)

======================================
David McCarter [Microsoft VB.NET MVP]
www.vsdntips.com
VSDN Tips & Tricks .NET Coding Standards available at:
www.cafepress.com/vsdntips.20412485
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top