How can I get the name of the calling function?

Fazzyer

Member
Joined
Jul 29, 2005
Messages
16
Programming Experience
5-10
Hi,

I tried a search, but it seems, that the problem has not yet been discussed here... :)

I'm currently writing a protocol-DLL for my projects; sometimes I find this very useful. It's just a simple one-class DLL with one function "WriteLine" that openes my protocolfile, writes the line of text I want and closes the file again. (I know that this is slow, but in bigger projects it helps very much if you can't find an error...)

At the moment, I'm passing the calling functions name and class as parameters to the function "WriteLine"... this is ok, but if I for what reason ever change a function name or move a function to another class, I always have to change all protocol-lines too.

Now I'm wondering, if there is a possibility to get the calling functions name and class just inside my WriteLine-function... this would be much easier. For example:

VB.NET:
' protocol-DLL, class Cprotocol
Private Sub WriteLine(ByVal Text As String)
    ' open file
    Stream.WriteLine(CALLINGFUNCTION & " from " & CALLINGCLASS & ": " & Text)
    ' close file
End Sub


Does anyone have an idea, how to do this or if this is possible?

Thanks, Fazzyer
 
pass the function's name as a string and the class name as a string to your writeline method
VB.NET:
' protocol-DLL, class Cprotocol
Private Sub WriteLine(ByVal CallingSub As String, ByVal CallingFunction As String, ByVal Text As String)
    ' open file
    Stream.WriteLine(CallingFunction & " from " & CallingSub & ": " & Text)
    ' close file
End Sub
it means that whenever the method is called, the person using the class would have to supply the correct information (which there wouldn't be a reason for them to not want it to be correct, it is their app)
 
Hi,

thanks for the quick answert!

Your solution to my problem is exactly the way, I'm doing it at the moment. :) Sorry if I wrote my question a bit confused... I wanted to know if there is a way to do this not in this way, to be more precise: if "WriteLine" can by itself retrieve the name of the function, it was called by.

Right now I try to teach myself the basics of System.Reflection-Class, to see if there is a way to realise my problem. Perhaps anyone has another idea or is familiar with System.Reflection and can tell me "do it this or that way" or "there is no solution for your problem".

Thanks, Fazzyer
 
You can't use reflection to get the requested info.
 
Hi,

thank you all for your answers!

I've tried the solution JohnH posted (StackTrace) and it works so far... I'll have to try a bit further, but it seems to be the thing I wanted!

Thanks again, Fazzyer
 
Back
Top