RamziAbb
October 18th, 2004, 01:43 PM
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
DSJ
October 19th, 2004, 08:24 AM
Look at the StackTrace class.
RamziAbb
October 19th, 2004, 09:54 AM
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)