473,372 Members | 1,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 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 1987
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Spry | last post by:
Hi, I wanted to write macros for finding the number of memory allocations and deallocations also wanted to find the locations. The code I have is a pretty big one. I have a wrapper on top of...
9
by: qazmlp | last post by:
How exactly __FILE__ and __LINE__ macros are defined? Or, Is the definition of these macros implementation dependent ? I am wondering how easily they can get the file name and line number...
5
by: jake1138 | last post by:
I couldn't find an example of this anywhere so I post it in the hope that someone finds it useful. I believe this is compiler specific (I'm using gcc), as C99 defines __VA_ARGS__. Comments are...
5
by: baumann.Pan | last post by:
where are these macros defined? can I use it a release ver code?
7
by: Kenneth Brody | last post by:
Am I correct that using __FILE__ and __LINE__ within a macro will expand to the filename and line in which the macro is invoked, rather than where it is defined? For example, in a header file: ...
19
by: v4vijayakumar | last post by:
why the following statement dumps the core(Segmentation fault)? printf("%s\n", __FILE__);
4
by: Joakim Hove | last post by:
Hello, i have simple function like this: def log_msg(msg , file , line): print "%s:%s %s" % (file,line,msg) the file and line arguments should be the filename and linenumber of the...
5
by: Neo | last post by:
Hie, Can I put __FILE__ and __LINE__ macros inside the class function which may not be inline. And void function() { classobject::LogInfo(...); } Internally LogInfo should log file name...
9
by: Neo | last post by:
Hi Friends, I am planning to use "__FILE__,__LINE__,__FUNCTION__ " for a logging component in my class. In debug build I gets all information. I tried with release mode also and it works. But I...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.