Best vb6 questions in October 2011

In ASP, C#, and VB.Net how to retrieve current line number

4 votes

Does ASP, C#, VB.NET have a way to retrieve what line its on in code as its processing commands?

Example

1 <%
2 response.write("Your on line " & retreiveCurrentLineNumber)
3 %>

Output: Your on line 2

You can do this:

var line = new StackFrame(0, true).GetFileLineNumber();

Note there are several caveats to this.

  1. You will need to make sure the source file and PDB are reachable.
  2. This will get you the current line of the method you are in, not exactly where you are.
  3. The Jit may perform optimizations that result in incorrect information, such as a method being inlined.

For VB.NET it's the same thing:

Dim line As Integer = New StackFrame(0, True).GetFileLineNumber()

As far as Classic ASP goes - I don't believe this is possible.