CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2001
    Posts
    61

    Getting the Line Of Code and File Name

    For debugging purposes, I want to pop up a message box with the Line of Code and File Name from which it was called. I can't find the call in VB that returns the current line of code or the call that returns the file name.

    Thanks,
    Ramzi

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Getting the Line Of Code and File Name

    Look at the StackTrace class.

  3. #3
    Join Date
    Oct 2001
    Posts
    61

    Re: Getting the Line Of Code and File Name

    Thanks a lot, that property had everything I needed.

    The following code will parse it out and pop it up.

    ' Initializes variables to pass to the MessageBox.Show method.
    Dim intLineStart As Integer
    Dim strLineOfCode As String
    Dim strFileName As String
    Dim strMessage As String
    Dim strCaption As String

    intLineStart = ex.StackTrace.IndexOf(":line ")
    strLineOfCode = ex.StackTrace.Substring(intLineStart + 6)
    strFileName = ex.StackTrace.Substring(ex.StackTrace.IndexOf(" in ") + 4, ex.StackTrace.Length - ex.StackTrace.IndexOf(" in ") - (ex.StackTrace.Length - intLineStart) - 4)
    strMessage = "Occurs in: " + strFileName + " Line of Code: " + strLineOfCode
    strCaption = "Exception Location"

    'Displays a MessageBox using the Question icon and specifying the No button as the default.
    MessageBox.Show(strMessage, strCaption, MessageBoxButtons.OK, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)

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

Featured