Calling method name

Is it possible to know in a method which method or function it is called from ?

For example ,
I have a ‘Method1’ where in the code ‘Method2’ is called.
Is there a way to see in Method2 That it was called from Method1 ?

Thanks

One way is to raise an exception, immediately catch it and examine the stack…

Another would be to pass CurrentMethodName as a parameter of the method.

Thanks.

Is it possible to have a code example to do this ?
Thanks.

Method2(CallingMethodName As String)
// Do something with name of calling method
Return

Method1()
Call Method2(CurrentMethodName) // Passes "Method1" in the parameter

Method3()
Call Method2(CurrentMethodName) // Passes "Method3 " in the parameter

Something like (forum code, not tested):

Try
   Raise new RuntimeException
Catch e
   // examine e.Stack
End

While I know there is no “better” solution than this, I always find it awkward to deliberately create an exception to know something.

I agree with that.
That’s why I went for the solution that I had first in mind : passing the ‘currentMethodName’ to the method.call
The only thing is that it will cost me hours (days) of work to change all my call’s.

Regards
.

instead of add it to every method call.
you could also log each method call if you need this for tracking.
Something like IAmHere(CurrentMethodName)
put it on a list and remove the first entry if list get over 100 or so.
u can use conditional compiling for skip this in release.

why you need to know in a method which method or function it is called from?

My app is using a dll with a lot of function for working with midi-files.
Each function returns a integer 0 if ok and another number if an error occured.

The functions in the dll are like this,

Declare the DLL method
Then call the method with ,

rtn = callDllMethod(parameters)
Return rtn '0 if NO_ERROR , other if ERROR

A DLL function is called like this in my app,

rtn = someDllFunction(some parameters)
If rtn > 0 Then someErrorCode

I was willing to get rit of the second line and put this check in the DLL method code

When a error occured in the DLL method call, I needed the method that did call this DLL method, to show in a messagebox.

Regards

i think what you want is something like this without input the string name

rtn = someDllFunction(some parameters)
If rtn > 0 Then ShowError("someDllFunction",rtn)

generally your error message need only the place where it happens and why,
if you can modify the someDllFunction method.

if the method have done her task with returning a boolean or a number or string is very useful for the following programming.

how about set a current task name in your dll library? i understood that you wrote own midi library
and in case of a error it show up too.

someDllTask("Set Volume")
rtn = someDllFunction1(some parameters)
rtn = someDllFunction2(some parameters)
rtn = someDllFunction3(some parameters)
rtn = someDllFunction4(some parameters)

Or implement a stack. On entry to any method, push its name onto the stack.

Since this is the “exceptional case”, ie., an error condition, why not use an exception? Rather than modify all your code.

To catch the name of the method which it is called from I’ve copied from this forum some time ago this:

Public Function Callstack() As String( )
  // returns the signatures of the current CallStack
  // workaround for <feedback://showreport?report_id=14501>
  
  Dim result() As String
  
  Try
    #Pragma BreakOnExceptions OFF
    Raise New RuntimeException
  Catch e As RuntimeException
    Dim stack() As String = e.Stack
    If stack.Ubound >= 2 Then
      // 0 is this Method
      // 1 is requesting Method
      // 2 is first calling Method
      stack.Remove(0)
    End If
    result = stack
  Finally
    #Pragma BreakOnExceptions Default
  End Try
  
  Return result
End Function

Public Function CalledBy() As String
  // returns the signature of the method that called the current method
  // similar to CurrentMethodName
  // this is a String, not an Object nor Introspection Reference. So IsA queries are not possible on the result
  
  Dim stack() As String  
  stack = CallStack
  
  // 0 is this mehthod
  // 1 is the method that called this
  // 2 is the method that was asked for
  Select Case stack.Ubound 
  Case Is >= 2
    Return stack(2)
  Case 1 
    //this can happen if we are called from the run event of an thread
    Return stack(1) 
  End Select
End Function

And for testing this methods you need only:

var test as string = CalledBy( )

Thanks Michael, I will try it.
Regards

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.