469,313 Members | 2,603 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Determining Line Number

VS 2003, vb.net...

Is there a way to determine the line number currently executing in your
code, so you can log this line number information (e.g. via trace)?

Thanks!

Bob Day
Nov 20 '05 #1
5 5788
"Bob Day" <Bo****@TouchTalk.net> schrieb:
VS 2003, vb.net...

Is there a way to determine the line number currently
executing in your code, so you can log this line number
information (e.g. via trace)?


You can set error numbers by hand:

\\\
Public Sub Bla()
1: Dim i As Integer
2: i = 2
3 Dim s As String = "Hello World"
End Sub
///

When using 'On Error...' error handlers, you can determine the line number
by reading the 'Erl' property of the 'Err' object.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #2
"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> schrieb:
3 Dim s As String = "Hello World"


Sorry, the line above should read "3: Dim s As String = "Hello World"".

;-)

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #3
Hi Bob,

The following will give you the line number of your code (as in the source
file):

Dim CurrentStack As System.Diagnostics.StackTrace
MsgBox (CurrentStack.GetFrame(0).GetFileLineNumber)

In case you're interested, you can find out about the routine that you're
in as well as all its
callers.

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
Return "In " & Myself & vbCrLf & "Called by " & MyCaller
End Function

This can be very handy if you want a generalised error routine because it
can get the name of the caller (which would be where the error occurred).

Regards,
Fergus
Nov 20 '05 #4
Thanks for your help.

Howver, using the code you suggest below in various situations always
returns a line number of zero
Dim CurrentStack As System.Diagnostics.StackTrace
MsgBox (CurrentStack.GetFrame(0).GetFileLineNumber)
Any suggestions?

Bob Day

"Fergus Cooney" <fi******@tesco.net> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl... Hi Bob,

The following will give you the line number of your code (as in the source file):

Dim CurrentStack As System.Diagnostics.StackTrace
MsgBox (CurrentStack.GetFrame(0).GetFileLineNumber)

In case you're interested, you can find out about the routine that you're in as well as all its
callers.

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 Return "In " & Myself & vbCrLf & "Called by " & MyCaller
End Function

This can be very handy if you want a generalised error routine because it can get the name of the caller (which would be where the error occurred).

Regards,
Fergus

Nov 20 '05 #5
Good question. yes, I am running in debug mode. The only thing I could
find on "Program Debug Symbols" in help was:
"To access the Debug Symbol Files property page, right-click on your
Solution in Solution Explorer and choose Properties from the shortcut menu.
Expand the Common Properties folder and click the Debug Symbol Files page. "

However, the "Debug Symbol Files" does not appear as an option as it says it
should above. I presume that means I some how heave it turned off. I don't
see where to turn it on. Where do you turn it on?

Please advise. Thanks for your help.

Bob Day

"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> wrote in message
news:up**************@tk2msftngp13.phx.gbl...
Hello,

"Bob Day" <Bo****@TouchTalk.net> schrieb:
Howver, using the code you suggest below in various situations always
returns a line number of zero
Dim CurrentStack As System.Diagnostics.StackTrace
MsgBox (CurrentStack.GetFrame(0).GetFileLineNumber)


Do you run your application in Debug mode? 'GetFileLineNumber' is only
avaliable if Program Debug symbols are available for the application.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet

Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by David Abrahams | last post: by
2 posts views Thread by johnivey | last post: by
7 posts views Thread by Daniel | last post: by
4 posts views Thread by Jeff Jarrell | last post: by
11 posts views Thread by Horacius ReX | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.