By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,146 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,146 IT Pros & Developers. It's quick & easy.

Getting current file name and Line number

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
> 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.