Is there a way to refer to the Function/Sub name?

D

Dean Slindee

Is there a way to refer to the name of the function the code is currently
within? I would like to identifiy on a MessageBox the function or sub
containing the message box without hardcoding it in a string, something like
this:

MessageBox.Show("The message to the user." & ControlChars.NewLine & _

"From:" & Me.Name & Function.Name, "Message Title", buttons)
 
M

Mythran

Dean Slindee said:
Is there a way to refer to the name of the function the code is currently
within? I would like to identifiy on a MessageBox the function or sub
containing the message box without hardcoding it in a string, something
like
this:

MessageBox.Show("The message to the user." & ControlChars.NewLine & _

"From:" & Me.Name & Function.Name, "Message Title", buttons)

Dean,

Dim funcName As String = _
System.Reflection.MethodInfo.GetCurrentMethod().Name

MsgBox( _
"The message to the user." & vbNewLine & _
"From: " & Me.Name & "." & funcName, _
"Message Titel", _
buttons _
)

HTH :)

Mythran
 
Z

Zoury

Hi Dean !

You may use the System.Diagnostics.StackTrace class for this :
'***
Option Explicit On

Module Module1

Sub Main(ByVal args As String())

Func1()
Console.WriteLine(New StackTrace().GetFrame(0).GetMethod().Name)

Console.WriteLine()
Console.WriteLine("Press Enter to quit...")
Console.ReadLine()

End Sub

Sub Func1()
Func2()
Console.WriteLine(New StackTrace().GetFrame(0).GetMethod().Name)
End Sub

Sub Func2()
Console.WriteLine(New StackTrace().GetFrame(0).GetMethod().Name)
End Sub

End Module
'***
 
Z

Zoury

System.Reflection.MethodInfo.GetCurrentMethod().Name

Well... I did learn something today ! <vbg>
 
M

Mythran

Zoury said:
Well... I did learn something today ! <vbg>

:) I use Reflection a lot for internal utility routines I have for my
testing applications (Console programs I use to help others in the
newsgroups...) I also had the need to print out the current method name and
didn't like having to type up the name of the method I was currently in for
every method...

Mythran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top