471,315 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,315 software developers and data experts.

Is there anything like __FILE__ and __LINE__ ?

wsq
in C++ we can use __FILE__ and __LINE__ to help us locating the bug, is
there anything like these in C# ?

thanks,
wsq
Feb 15 '06 #1
3 1874
Hello wsq,

Unfortunatelly .NET have no similiar macros.
To get this functionality u have to use StackTrace class

Smth like this
using System;
using System.Diagnostics ;
namespace getline
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
StackTrace st=new StackTrace (0,true);
StackFrame sf=new StackFrame ();
sf=st.GetFrame (0);
Console.WriteLine ("FileName: {0}",sf.GetFileName
());
Console.WriteLine ("Line Number:
{0}",sf.GetFileLineNumber ());
Console.WriteLine ("Function Name:
{0}",sf.GetMethod ());
Console.Read ();
}
}
}

w> in C++ we can use __FILE__ and __LINE__ to help us locating the bug,
w> is there anything like these in C# ?

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Feb 15 '06 #2
wsq
> Hello wsq,

Unfortunatelly .NET have no similiar macros.
To get this functionality u have to use StackTrace class

Smth like this
using System; using System.Diagnostics ;

namespace getline { class Class1 {
[STAThread] static void Main(string[] args)
{ StackTrace st=new StackTrace
(0,true);

StackFrame sf=new StackFrame ();
sf=st.GetFrame (0);
Console.WriteLine ("FileName: {0}",sf.GetFileName ());
Console.WriteLine ("Line Number:
{0}",sf.GetFileLineNumber ());
Console.WriteLine ("Function Name: {0}",sf.GetMethod ());

Console.Read (); } }

}

w> in C++ we can use __FILE__ and __LINE__ to help us locating the bug,
w> is there anything like these in C# ?

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do
not cease to be insipid." (c) Friedrich Nietzsche


Thank you, Michael, it works, but only in debug version. In release
version, GetFileName() always return zero. Am I missing something?

regards,
wsq
Feb 15 '06 #3
No, you're not. Release assemblies don't contain enough information to
relate a stack trace back to the precise source file name and line
number.

The only reason that this worked in C / C++ is that these were macros
that were resolved at compile time. C# resolves its file name and line
number information at run time, and so depends upon symbolic
information available (or not) at run time.

Feb 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by qazmlp | last post: by
7 posts views Thread by Kenneth Brody | last post: by
19 posts views Thread by v4vijayakumar | last post: by
4 posts views Thread by Joakim Hove | last post: by
5 posts views Thread by Neo | last post: by
9 posts views Thread by Neo | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.