Hi all,

I need to pass an event as a parameter to a function and I'm wondering if there is any way of doing this.

The reason why I need to do this is because I have a sequence of two lines of code that is littered all over my program, where I dynamically remove the handler to an event, then set the handler again. I'm doing this for several different events and event handlers, so I've decided to write a function that does this.

As an example, let's say I have a combobox in my code called combobox1, and I have the handler called indexChangedHandler. In several places of my code, I have the following two lines:

RemoveHandler combobox1.SelectedIndexChanged, AddressOf indexChangedHandler
AddHandler combobox1.SelectedIndexChanged, AddressOf indexChangedHandler

Now, I don't want to keep on repeating the above two lines of code (or similar) all over my program, so I'm looking for a way to do this:

Private Sub setHandler(evt As Event, hndler As eventhandler)
        RemoveHandler evt, hndler
        AddHandler evt, hndler
 End Sub

so that everywhere where those two lines of code(or similar) occur in my program, I can just replace them with:

setHandler(combobox1.SelectedIndexChanged, AddressOf indexChangedHandler)

So far, the "evt as Event" part of the argument of the setHandler function is giving an error. Any ideas on the right way to do this will be very welcomed. Thanks.

What error are you getting, exactly? Offhand, I don't see why that wouldn't work.

What error are you getting, exactly? Offhand, I don't see why that wouldn't work.

The thing is, Event is not a type. so when I put

evt As Event

, I was simply trying to give an idea of what I'm trying to achieve. I've gotten answers from other forums though, that an event is not a type and thus cannot be passed as an argument.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.