Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6953707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:30:50+00:00 2026-05-27T14:30:50+00:00

I am trying to create a Sub that can take an object and add

  • 0

I am trying to create a Sub that can take an object and add a load of event handlers for it, but the object can be any one of N types that all have these events. How can I do this? If I just have it as an Object as below AddHandler complains that the event does not exist for Object.

Can this be done?

Sub addHandlers(ByVal inputObject as object)
    AddHandler inputObject.MouseDown, AddressOf HandleClick
    AddHandler inputObject.MouseUp, AddressOf HandleMouseUp
End Sub

EDIT: Each of these objects is an instance of a class that inherits a different class (usually a control) that already has these events.

Thanks for any help,
Sam.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:30:50+00:00Added an answer on May 27, 2026 at 2:30 pm

    You probably just want to make inputObject a type that has all of these members. For example, if your using WinForms, it would be Control:

    Sub addHandlers(ByVal inputObject As Control)
        AddHandler inputObject.MouseDown, AddressOf HandleClick
        AddHandler inputObject.MouseUp, AddressOf HandleMouseUp
    End Sub
    

    Or if WPF, use UIElement:

    Sub addHandlers(ByVal inputObject As UIElement)
        AddHandler inputObject.MouseDown, AddressOf HandleClick
        AddHandler inputObject.MouseUp, AddressOf HandleMouseUp
    End Sub
    

    Now any control that is of that type can be passed into addHandlers.

    EDIT:

    If there is no common type between all of them, you could use reflection:

    Sub addHandlers(ByVal inputObject As Object)
        Dim type = inputObject.GetType()
        Dim handleClickDel = New MouseEventHandler(AddressOf HandleClick)
        Dim handleUpDel = New MouseEventHandler(AddressOf HandleMouseUp)
        type.GetEvent("MouseDown").AddEventHandler(inputObject, handleClickDel)
        type.GetEvent("MouseUp").AddEventHandler(inputObject, handleUpDel)
    End Sub
    

    The delegate type must still match though. This feels a bit clunky to me, though. If possible, try creating different overloads for addHandlers as needed, and using base types to avoid creating many of them. If own the code of the types that can be passed in as inputOjbect, use an interface.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create a user account in a test. But getting a Object reference
I'm trying to create a batch script that will delete all sub directories which
I'm trying to create a sub-class of the JButton component that will enable or
I am trying to create a second thread with dispatcher so that I can
I'm trying to create a class that serves as a base object, which will
I'm having trouble creating a sub that can create objects of a variable type
I'm trying to create a program that can be used by other people. Currently,
I am trying to create a multi project template. I wish the sub projects
I am trying create a WCF service that leverages the WPF MediaPlayer on the
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.