Optionally AddHandler

Is there a way that I can optionally add an event handler ?

Below is something like what I'd like to accomplish.  The obvious problem here is that if the button does not require a Handler then there won't be a btn_Click method.  Any ideas ?

Public Sub addBtn(content As Object, name As String, hasHandler As Boolean)
        Dim btn As New Button
        btn.Content = content
        btn.Name = name
        If hasHandler Then
            AddHandler btn.Click, AddressOf btn_Click
        End If
    End Sub

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Well what you show is exactly how you would do it. Can I assume that you've a set of predefined handler functions declared in your code? If not, then how are you declaring the handlers?
WatersideAuthor Commented:
Ah there lies the problem.  If hasHandler is False then there won't be a btn_Click.
käµfm³d 👽Commented:
Understood (somewhat), but if hasHandler is False, then you won't enter that block anyway. So I'm missing something here.
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

WatersideAuthor Commented:
The compiler can't find the handler.
käµfm³d 👽Commented:
In theory, I understand what you are saying, but based on the code example that you are displaying, this wouldn't happen. A handler can't exist at one moment in code and then not exist at another moment. If there is a function called btn_Click in your code when you compile, then it's always going to be there regardless of what the If is doing. So I'm thinking that your example isn't doing the question justice. If I'm mistaken, then please let me know, but maybe a different example is needed.

Now, if you were using delegates or labmdas, then I could see where you might have this issue.

Can you maybe rephrase either the question or the example?
Jacques Bourgeois (James Burger)PresidentCommented:
Same as kaufmed, your question might not be clear because your code does exactly what you seem to be asking for.

Maybe what you intend to say it that the event fires even your hasHandler parameter is false and you do not set the handler through your code. This would be the case if you created the Click event in the usual way, but did not remove the Handles clause at the end of the btn_Click declaration line. This clause automatically create a handler. If you want to have complete control on whether the event is activated or not, you should remove that Handles clause.

On other possibility is that the signature (the parameters) is not good for a Click event.

Putting these two possible problems together, your method declaration should look like the following:

Public Sub btn_Click(sender As Object, e As EventArgs) 'No Handles at the end

If this is not what you meant, then simply rephrase your question.
WatersideAuthor Commented:
Oh.  I just discovered that the handler can't 'see' btn_chick regardless of the state of hasHandler, so it's a scope issue ?

I can 'see' Public subs between class files but not those in code behind files.
käµfm³d 👽Commented:
You can only "see" them if you create an instance of the target class, or if you make them Shared methods. This is how all of .NET works. (Well, modules in VB.NET are a little different, but really they are just syntactic sugar for a Shared class.) However, since we are talking about event handlers, IMO it's a bad design to instantiate a form class just to gain access to one of its event handler methods. Instead, I would break out the common logic into a utility class. Then have each handler on each form call the method from the utility class.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WatersideAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.