Results 1 to 12 of 12

Thread: Determine which method called a method

  1. #1

    Thread Starter
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690

    Determine which method called a method

    I have a method (Log) that logs stuff to a text file. This gets called from numerous places. Is there a way, in the Log method, to determine the name of the method that called it?

    Thanks,
    Mike

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    There's no doubt better ways, but off the top of my head, you could pass the calling method's name as an argument. You could throw an exception in the method, catch it, and parse the exception.stacktrace, although that sounds like a lot of work.

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Isn't the identity of the caller in Sender?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  4. #4
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    Isn't sender for the object that fired an event, not a method?
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  5. #5

    Thread Starter
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Yeah, I'm lazy and did not want to send to the Log method the name of the method I'm in. One more thing to maintain if I change the method name.

    Parsing the stack trace is a smart idea, it's some work, but I'd only have to do it once and never touch it again.

    Taxes, what do you mean by the "identity of caller in Sender"? Can I pass an argument something like Me.CurrentMethodName?

    Thanks,
    Mike

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Check Reflection class , I think it's what you want .

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Try this...
    VB Code:
    1. Imports System.Reflection
    2.  
    3. Public Class Form1
    4.     Inherits System.Windows.Forms.Form
    5.  
    6.     '//Windows Form Designer generated code
    7.  
    8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    9.         A()
    10.     End Sub
    11.  
    12.     Private Sub A()
    13.         B()
    14.     End Sub
    15.  
    16.     Private Sub B()
    17.         C()
    18.     End Sub
    19.  
    20.     Private Sub C()
    21.         D()
    22.     End Sub
    23.  
    24.     Private Sub D()
    25.         E()
    26.     End Sub
    27.  
    28.     Private Sub E()
    29.         Dim st As New StackTrace
    30.  
    31.         For x As Integer = 0 To st.FrameCount - 1
    32.             Diagnostics.Debug.WriteLine(st.GetFrame(x).GetMethod.Name())
    33.         Next
    34.  
    35.     End Sub
    36.  
    37. End Class
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Mike Hildner

    Taxes, what do you mean by the "identity of caller in Sender"? Can I pass an argument something like Me.CurrentMethodName?

    Thanks,
    Mike
    I thought Methods, Subs & Functions were virtually the same. When a Sub or Function is called you can read the Sender.Name property. Or am I wrong?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  9. #9

    Thread Starter
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    crptcblade,

    Most excellent answer. I did not even know about the StackTrace and StackFrame classes. Much appreciated. Clean, simple way to do it (I was already writing a StackTraceParser class). As a bonus, I learned something new. Nice.

    Thanks crptcblade,
    Mike

  10. #10
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950
    Well, sure, you could get an answer from people who know what they're talking about. But what would that do to my post count?

  11. #11

    Thread Starter
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    LOL, salvelinus. That's why forums are good. At least you offered a good idea. Like I said, I was already writing a parser class, which, I thought would be kinda fun - regular expressions and all. Of course, not having to write it is even better. I appreciate your response to my original question. I didn't think of it.

    Thanks,
    Mike

  12. #12

    Thread Starter
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Pretty interesting what you find - the method names that is. I'm using this code:
    VB Code:
    1. Dim callingMethodName As String
    2. Dim st As New StackTrace
    3. callingMethodName = st.GetFrame(st.FrameCount - 1).GetMethod.Name

    This is a service, when I call the Log method from the OnStart method, the name of the method is actually ServiceQueuedMainCallback. When called from the OnStop method, the method name is Main. Even stranger, I have a method named ReadClientCallback, which shows up as CompletionPortCallback. All the other methods show up as the name I've given them.

    Don't think it's anything to worry about, but it is strange.

    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width