Suppress warning from a single line

K

Kim Hellan

I want to suppress the following warning when compiling.
warning CS0169: The private field 'myvar' is never used

In C++ you would do something like (not sure of the syntax):
#pragma nowarn:0169
string myvar;
#pragma warn:0169

Is this really still not possible in C#?
I know you can set the /nowarn:0169 option in you project, but I do not want
to disable it for everything in the project..
I just want to disable it from that one line until 'myvar' gets implemented.
 
N

Nicholas Paldino [.NET/C# MVP]

Kim,

You can not do this in C#, you will have to disable the warning.

Of course, the warnings are there for a reason, and the fix is simple
for your particular warning, so it's probably better that you code the
variable declaration the right way.

Hope this helps.
 
K

Kevin Spencer

Surer Kim. Use:

#pragma warning disable
#pragma warning restore

You can also use this directive with a comma-delimited list of warning
numbers. Example:

#pragema warning disable 0169

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
B

Bill Butler

Kim Hellan said:
I want to suppress the following warning when compiling.
warning CS0169: The private field 'myvar' is never used

In C++ you would do something like (not sure of the syntax):
#pragma nowarn:0169
string myvar;
#pragma warn:0169

Is this really still not possible in C#?

Sure it is. The syntax is far simpler as well (actually, you can do the same thing in C++)
just change
string myvar;
to
// string myvar;

There...problem solved.

OK, I am joking around a bit, but really, why not just comment it out until you are ready to use it?

No, really, I am sure you will have thought of commenting it out considering the depth of solution
you have persued.
What is wrong with the comment?

Bill
 
J

Jon Skeet [C# MVP]

Kevin Spencer said:
Surer Kim. Use:

#pragma warning disable
#pragma warning restore

You can also use this directive with a comma-delimited list of warning
numbers. Example:

#pragema warning disable 0169

Note that (as far as I can tell) this is for C# 2.0 only. It certainly
doesn't seem to work with the 1.1 compiler. (Interestingly enough, I
couldn't find it in my printed copy of the C# 2.0 spec I was looking at
earlier...)
 
K

Kevin Spencer

Darn, Jon! Most of the time I look in the correct version of the MSDN
library, but the one I use the most is the 2.0 version. I can assure you,
though, at least in .Net 2.0, it's there!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
J

Jon Skeet [C# MVP]

Kevin Spencer said:
Darn, Jon! Most of the time I look in the correct version of the MSDN
library, but the one I use the most is the 2.0 version. I can assure you,
though, at least in .Net 2.0, it's there!

Yup - I checked that it would work with 2.0 (at least the beta 2). I'm
still surprised I couldn't find it in the language spec though. Must
have a closer look tomorrow...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top