Cal.B Cal.B - 1 year ago 135 Question

VB.Net snippets - get function or sub name

It would be very helpful if visual studio 2015/ snippets are capable of getting the function name they're written in.
For example a function like so:

private function SomeFunctionA() as Boolean
<insert snippet here and it generates 'SomeText - SomeFunctionA>
end function

private sub aSubRoutine()
<insert snippet here and it generates 'SomeText - aSubRoutine>
end sub

If it's possible or not, any information would be very helpful (this is a tricky one to google!) thanks in advance.

PS - I'm aware of how to create snippets, but I don't know any syntax that can do the above.

Answer Source

You can do this using reflection.

Add this to the top of your file:

Imports System.Reflection.MethodBase

Then you can use the following Functions:

GetCurrentMethod().DeclaringType().Name 'Gets the name of the Class/Module

GetCurrentMethod().Name 'Gets the current Sub or Function name

So the following is what you need:

Private Sub aSubRoutine()
    MessageBox.Show(GetCurrentMethod().Name) 'Shows aSubRoutine
End Sub
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download