Passing a Method ?

Is there a way that I can add a property to a class that I can then use to fire given method ?

Public Class Box
   Public name As String
   Public size As Double
   Public mouseentermethod As Method ?????
End Class

Public Sub drawBox(box As Box)
        With box
                    Dim rect As New Rectangle
                    rect.Name = .name
                    rect.Width = .size
                    rect.Height = .size
                    rect.Fill = New SolidColorBrush(Colors.Red)

                    ' call the method associated with this instance of the Box Class
                    AddHandler rect.MouseEnter, AddressOf .mouseentermethod
        End With
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.

Éric MoreauSenior .Net ConsultantCommented:
this is exactly the definition of a Delegate. Check
WatersideAuthor Commented:
Ok.  What type do I make .mouseentermethod in the Class to be able to use it with the AddHandler ?
Éric MoreauSenior .Net ConsultantCommented:
I would point the AddHandler to a real method that exist in that class and that method would call the delegate.
WatersideAuthor Commented:
Can you elaborate please ?
Éric MoreauSenior .Net ConsultantCommented:
Try this:

Option Strict On

Public Class Form1

    Private myBox as Box
    Private Sub Button1_MouseEnter(sender As Object, e As EventArgs) 'Handles Button1.MouseEnter
        myBox.mouseentermethod(sender, e)
    End Sub

    Private Sub Button1_MouseLeave(sender As Object, e As EventArgs) Handles Button1.MouseLeave
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        AddHandler Button1.MouseEnter, AddressOf Button1_MouseEnter
        myBox = New Box()
        myBox.Name = "test"
        myBox.mouseentermethod = AddressOf CustomMouseEnter
    End Sub
    private Sub CustomMouseEnter(sender As Object, e As EventArgs) 
        Label1.Text = myBox.Name
    End Sub

End Class

Public Class Box
    delegate sub MyMouseEnter(sender As Object, e As EventArgs)

    Public Name As String
    Public Size As Double
    Public mouseentermethod As MyMouseEnter
End Class

Open in new window

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
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.