468,270 Members | 1,524 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,270 developers. It's quick & easy.

Getting current file name and Line number

Hi,
I am trying to get the current file and the line number which is
getting executed. I used:

MessageBox.Show(New StackTrace(New
StackFrame(True)).GetFrame(0).GetFileLineNumber) which gives me the
line number.

Later on, I was trying to write a generalized routine, so that I can
log the file name and the method.

<code>
Public Function MeAndMyCaller As String
Dim CurrentStack As New System.Diagnostics.StackTrace
Dim Myself As String = CurrentStack.GetFrame(0).GetMethod.Name
Dim MyCaller As String = CurrentStack.GetFrame(1).GetMethod.Name
Dim cur_file As String = CurrentStack.GetFrame(1).GetFileName
Dim line_num As Integer = CurrentStack.GetFrame(1).GetFileLineNumber
Return "In " & Myself & vbCrLf & "Called by " & MyCaller
End Function
</code>

Here, I get MyCaller correctly, but cur_file is not getting returned. I
get a Null exception. line_num is returned as 0, all the time.

What am I doing wrong? Any suggestions/ideas?

Regards,
P

Jun 16 '06 #1
1 1521
> I am trying to get the current file and the line number which is
getting executed. I used: .... Here, I get MyCaller correctly, but cur_file is not getting returned. I
get a Null exception. line_num is returned as 0, all the time.


Your symptoms are consistent with debug vs release configurations. Line
number is available in debug version, not available in release version.
Jun 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Larry Tate | last post: by
1 post views Thread by iwdu15 | last post: by
2 posts views Thread by karinmorena | last post: by
5 posts views Thread by tshad | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.