Delegates in VB.NET

Today I had use VB.NET to alter a gui component, and got curious about the usage of delegates for events.

When inheriting a class and wanting to "pass along" this class events it seems as one have to use something called shadows.

Public MyClass Inherits SomeClassThatImplementsTheClickEvent

     Public Shadows Event Click(ByVal sender As Object, ByVal e as EventArgs)

End Class

I would assume that the delegate type would be the same as for the base class, but it's not. When inspecting the resulting dll with the Object browser I found that it was created a "MyClass.EventNameEventHandler", aka "MyClass.ClickEventHandler" delegate for this event.

Is it correct that compiling VB.NET code generates a custom delegate, and if so why? 

Comments

# Scott Swigart said:

Not sure what you're trying to do. Do you want your derived class to fire base class events? If so, you don't need to re-declare them in the derived class. You can just RaiseEvent MyBase.Click(x,y).

Are you trying to do additional stuff when the Click event fires? If so, overload OnClick (make sure to call MyBase.Click in that method.)

In what you are doing, you are creating an event that only exists in the derived class. It happens to use the same name as an event in the base class, so you have to use shadows to let VB know "yes, I really want to declare a new thing with the same name as something else that's in the same scope."

Wednesday, April 23, 2003 4:32 AM
# Mads said:

thanks scott!

Actually, I don't really know what I'm trying to do as I was just trying to mimic how this component was made bye someone else.

But, when using "Shadows" does it automagically create the delegate?

Wednesday, April 23, 2003 4:46 AM
# Greg Robinson said:

my experience says 'yes'...why, casue like other things, vb hides a lot from you, good\bad\indifferent.

Wednesday, April 23, 2003 5:49 AM
# PETER PARERA said:

do we really have keywords such as delegate in vb.net?

Monday, June 02, 2008 11:41 PM