Getting current file name and Line number

pukya78

Member
Joined
Jun 5, 2006
Messages
15
Programming Experience
Beginner
Hi,
I am trying to get the current file and the line number which is getting exectued. It seems that we can only get the line number when an error occurs. Is there any other way to find the line numbeR? I want to log the line number when a call to any method is made.
Also, in Visual Studio, can we see the line numbers when we are writing our code? (similar to doing "se nu" in vi editor)

Regards,
P
 
For the line number when writing code : Tools/Options, then in the TreeView select Basic Text Editor/Editor then last CheckBox on the right is "Line number".

Names could be different since I am using the French version and have translated for you.
 
Hi,
Thanks for that. Can we get the line number programattically? I tried a few things but could only get the line number when an error occured.

Regards,
P
 
VB.NET:
[SIZE=2]MessageBox.Show([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] StackTrace([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] StackFrame([/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])).GetFrame(0).GetFileLineNumber)
[/SIZE]
 
Thanks, that worked.
Initially when I tried the code which you gave, it would lways give me line number as 0. Later i found that we can execute that code only in debug mode.
Regards,
P
 
Hi,
I was trying to write a generalized routine, so that I can log the file name and the method.

VB.NET:
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
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
 
Last edited by a moderator:
You have to use the StackTrace constructor that initialize the Boolean fNeedFileInfo parameter "True to capture file name, line number, and column number" as is says in documentation. So change to:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] CurrentStack [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Diagnostics.StackTrace([/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2])
[/SIZE]

 
dont do this in a production system.. it's what the debugger is for. just catch exceptions and show meaningful messages to the user, dont have the program constantly looking at what line and file it is - to maintin this info, much debug info will need to be stored with the exe because after the code is compiled.. well.. it looks nothing like what it does in the ide, thats for sure :)
 

Latest posts

Back
Top