Getting the Name of Calling Method

I have a Session Method that allows me to enter some Text as a parameter to insert into a record:

Sub MyMethod (note As Text) <Start of code that inserts stuff into a record> some Text + EndOfLine +_ note + EndOfLine +_ more Text <End of code that inserts stuff into a record> End Sub

I’d like to be able to have MyMethod insert CurrentMethodName of the calling Method as the note you see above, if note is empty:

Sub MyMethod (note As Text = CurrentMethodName) ... same as previous code ... End Sub

But Xojo won’t allow that during compilation. So for now, wherever I use MyMethod (note As Text), I must manually fill in CurrentMethodName for its parameter when I want that:


That works fine. I’m just trying make that the default when no parameter is supplied.

See the last post

You will need to put #Pragma BreakOnExceptions off at the top of the method to test it in the IDE.

Tested in windows, works fine.

Thanks, Julian. I saw that thread earlier, but wanted to see if I could just do it more simply. I thought maybe there was a trick that allows a Method to accept CurrentMethodName as a default value for a Parameter (As String or As Text).

While it’s not as an elegant solution, in the methods where I want to know the calling function name I have a property just for that.

sub myMethod( mainProps as , callingFunctionName as string )

You could then replace a blank value with the calling function name if that’s what you need.

It also helps me to remember which functions are using that function.

this came up just recently in the German forum:

I eventually decided to go ahead and handle this by creating a global function using the code in the German forum thread mentioned by Tobias. Very similar to the code cited by Julian.

Just FYI, testing with 10000 calls, using the exception method is approx 15000x slower than just passing the method name in when measuring in the debugger. In a 64-bit compiled app, it’s about 32000x slower.

Thanks, Greg. Though this is only being used to log DB errors, I may go back to just passing the method name as a parameter.

@Greg O’Lone,

Do you think a future version of Xojo will have a built-in CallingMethodName method?

[quote=373897:@Ralph Alvy]@Greg O’Lone,

Do you think a future version of Xojo will have a built-in CallingMethodName method?[/quote]
No. CurrentMethodName is literally a constant that gets rendered at the beginning of each method’s code.

It’s also worth noting that if you don’t include function names in your binary, the exception method probably won’t work anyway.