wpfvb.netvariablesdeclarationsender

How to get the Subroutine Sender name


When I click the Button1 the MsgBox shows me "1" which is good.

Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
    MsgBox(CType(sender, Button).Name.Replace("Button", ""))
End Sub

I want the MsgBox shows me "30" when I click the Button2.

Private Sub Button2_Click(sender As Object, e As RoutedEventArgs) Handles Button2.Click
    Hello30()
End Sub

Sub Hello30()
    'The following line is need to be repaired.
    MsgBox(CType(sender, ????).Name.Replace("Hello", ""))
End Sub

Solution

  • Not really sure what you're trying to do, but this is an example that'll get you close:

    Imports System.Reflection
            
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Hello30()
    End Sub
    
    Sub Hello30()
        Dim method As String = MethodBase.GetCurrentMethod.Name
        MsgBox(method.Replace("Hello", ""))
    End Sub