tags:

views:

436

answers:

4

See this example:

''//file1.vb
Sub Something()
    ''//...
    Functions.LogInfo("some text")
    ''//...
End Sub

''//functions.vb
Sub LogInfo(ByVal entry as String)

    Console.WriteLine(entry)

End Sub

Can I get the the name "Something" inside LogInfo?

Sorry for the briefness of this post, I am not sure how to properly articulate this question. I will clarify and elaborate as needed.

+6  A: 

(EDIT: Removed unnecessary use of StackTrace itself - which would be useful if you wanted to print out more info than just one frame.)

You can use the StackFrame class, but it's pretty expensive (IIRC) and may be slightly incorrect due to inlining.

EDIT: Something like this: (the NoInlining is to make sure it behaves properly...)

Imports System.Diagnostics
Imports System.Runtime.CompilerServices

Public Class Test

    Shared Sub Main()
        Something()
    End Sub        

    <MethodImpl(MethodImplOptions.NoInlining)> _
    Shared Sub Something()        
        Functions.LogInfo("some text")
    End Sub

End Class

Public Class Functions

    <MethodImpl(MethodImplOptions.NoInlining)> _
    Public Shared Sub LogInfo (ByVal entry as String)
        Dim frame as StackFrame = new StackFrame(1, False)
        Console.WriteLine("{0}: {1}", _
                          frame.GetMethod.Name, _
                          entry)
    End Sub

End Class
Jon Skeet
Downvoters: any particular reason?
Jon Skeet
+4  A: 

Take a look at How can I find the method that called the current method?.

Translated to VB (Hopefully):

Imports System.Diagnostics
''// get call stack
Dim stackTrace As New StackTrace()

''// get calling method name
Console.WriteLine(stackTrace.GetFrame(1).GetMethod().Name)
Quintin Robinson
thanks that worked out just as I needed. I wonder why that post didnt come up in my searches...
Anders
The only reason I was able to find it is because I had seen it before, I had to search for StackTrace and GetFrame just to get it to show up. I would recommend you take Jon's advice on making sure you are getting the correct frame, he's the .NET expert IMO.
Quintin Robinson
You can do better than this- it's possible to only need to create an instance of the exact stack frame you need rather than clone the entire stack.
Joel Coehoorn
I agree Joel, I really just copied/translated the code from the post I referenced.
Quintin Robinson
Note that if you don't need the file name, you can use an overload to say that, which is likely to be cheaper. (My guess is it doesn't need to hit the PDB file.) See my answer for an example.
Jon Skeet
A: 

You can use the StackFrame or StackTrace. But your behavior might different in release builds vs running in the debugger due to inlining. So your results might be not what you expect:

c#:

System.Diagnostics.StackFrame sf = new StackFrame(1);
sf.GetMethod().Name;

vb:

Dim sf as new StackFrame(1)
sf.GetMethod().Name

Edit

Sorry just realized you asked for vb.net. I edited my response but the vb syntax might not be right

JoshBerke
A: 

System.Reflection.MethodBase.GetCurrentMethod.Name does the same thing and is seemingly easier to get the hang of