Cal.B Cal.B - 4 months ago 26
Vb.net Question

VB.Net snippets - get function or sub name

It would be very helpful if visual studio 2015/VB.net 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

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
Comments