Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Access to current sub/function name?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Resolved [RESOLVED] [2005] Access to current sub/function name?

    In debugging a recent program I found it useful to add a line at the top of every function and sub such as;
    VB Code:
    1. Private Sub MySub
    2. If DebugMode Then WriteToLog("MySub")
    By scanning down the log I can see the sequence of functions/subs that are running. So I can check that things are happening in the right order, no multiple loops, events are firing etc. Is this something I can already do in the IDE?, I can't see anything like this in the menus.

    Also, is there a string function or something that returns the name of the current sub/function? So I could do something like;
    VB Code:
    1. Private Sub MySub
    2. If DebugMode Then WriteToLog(NiftyFunction.CurrentSubFunction)
    instead of having to add the debug lines mentioned individually?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Access to current sub/function name?

    When you hit a break point you can look at the Call Stack window, but it will only go back as far as the most recent Windows message that prompted an action.

    As for your second question, look at the StackTrace class. While you're there, explore the System.Diagnostics namespace.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Access to current sub/function name?

    Also, don't use code like this:
    VB Code:
    1. If DebugMode Then WriteToLog("MySub")
    If you're not going to use the Debug class itself, or the Trace class, then use conditional compilation so the code won't even exist in a Release build:
    VB Code:
    1. #If DEBUG
    2.     WriteToLog("MySub")
    3. #End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] Access to current sub/function name?

    Ah, ok thanks. So for posterity, this is what I ended up with...

    VB Code:
    1. Public Class Form1
    2.  
    3. #If DEBUG Then
    4.     Dim LogFile As String = Application.StartupPath & "\" & "LogFile.txt"
    5.  
    6.     Public Sub WriteStackTrace()
    7.         Dim StackTrace As New StackTrace(True)
    8.         Dim sw As System.IO.StreamWriter
    9.         Dim str As String()
    10.         'Create or Append
    11.         sw= New System.IO.StreamWriter(LogFile, System.IO.File.Exists(LogFile))
    12.         str = StackTrace.GetFrame(1).ToString.Split(" ")
    13.         'Strip off the function/sub name (there is more info than just that available)
    14.         sw.WriteLine(Date.Now.TimeOfDay.ToString & " " & str(0))
    15.         sw.Close()
    16.     End Sub
    17.  
    18. #End If
    19.  
    20.     'The load event
    21.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    22.  
    23. #If DEBUG Then
    24.         'Delete an existing log file
    25.         If System.IO.File.Exists(LogFile) Then System.IO.File.Delete(LogFile)
    26.         WriteStackTrace()
    27. #End If
    28.  
    29.         'The program
    30.  
    31.         Me.Close()
    32.     End Sub
    In each sub/function put;
    VB Code:
    1. #If DEBUG Then
    2.         WriteStackTrace()
    3. #End If
    Which will write out a logfile showing the current time and the name of the function/sub running. Since the code added to each function/sub is always the same, this can be easily edited into an existing program. I tested this and it works, but if my code is no good then please comment.

    An enhancement would be to subtract the times, then you can see how long each sub takes.
    Last edited by Bulldog; Feb 19th, 2007 at 07:06 PM.

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