Know who is calling a method

I have a method called from many other methods.
Is there a way to get the caller method name by code inside the called method?
I would like to avoid to set an optional parameter (for example: Optional callerName As String) in my caller method.

Thanks a lot. It’s exactly what I’m looking for.

xDev 13.6 Nov/Dec 2015 Tip 5: Who’s calling?

Sometimes you come across a question and shortly after you come across an answer to the question. That happened again when a few weeks ago I saw a thread on the forum where people debugging a problem wished for a simple way to know from where their methods were called. And then I ran across a [blog post] ( on the old REALbasic site where Thomas Tempelman addressed exactly that problem in quite a nifty way.

Basically he uses the fact that instances have a scope, a limited life time that ends when the method or event they are created in terminates. So he creates a MethodProfiler class with a Constructor and a Destructor that do the logging. When you instantiate a new MethodProfiler object then you pass it the name of the current method like this:

    Sub myMethod()
        dim myTmpObject as new MethodProfiler(CurrentMethodName)

Consequently when you create the class at the beginning of a method then the Constructor logs where that happens, and it stores the passed method name in its own mName property for later use in the Destructor:

    Sub Constructor(name as String)
        System.DebugLog "<"+name+"> Entered"
        mName = name
    End Sub

At the end of the method the MethodProfiler instance is destroyed and the Destructor is called - which logs that too:

    Sub Destructor()
        LogMsg "<"+mName+"> Exited"
    End Sub

So in the log you can easily follow your apps progress.

But you can do much more with it too:

  • you can globally enable/disable debugging
  • you could measure the time that the method takes to execute
  • you could count the depth, allowing you to indent the output, making it more readable if you have nested calls you want to trace.

For your convenience, Thomas made a demo project with a ready-to-use MethodProfiler class for you to [download] (

dim methodCaller as string try dim n as new NilObjectException Raise n catch err as NilObjectException dim s() as String = err.Stack s = Split(s(1),"%") methodCaller = s(0) end try

This code works fine on linux/mac but on windows err.stack is an empy rows array.

You enabled compiler to include function names?

You’re right as always! Using “include function names” works also on windows.

Great Christian!

Thanks a lot.


I’ve been at this since REALBasic 4 and I constantly forget that for debugging …