Removing Warnings from CSharp Compile Cycle

File name            	:DSCN4983.JPG</p> <p>File size            	:391.4KB(400749Bytes)</p> <p>Date taken           	:0000/00/00 00:00:00</p> <p>Image size           	:1600 x 1200</p> <p>Resolution           	:300 x 300 dpi</p> <p>Number of bits       	:8bit/channel</p> <p>Protection attribute 	:Off</p> <p>Hide Attribute       	:Off</p> <p>Camera ID            	:N/A</p> <p>Camera               	:E775</p> <p>Quality mode         	:NORMAL</p> <p>Metering mode        	:Matrix</p> <p>Exposure mode        	:Programmed auto</p> <p>Speed light          	:No</p> <p>Focal length         	:16.7 mm</p> <p>Shutter speed        	:1/417second</p> <p>Aperture             	:F4.8</p> <p>Exposure compensation	:0 EV</p> <p>White Balance        	:Auto</p> <p>Lens                 	:Built-in</p> <p>Flash sync mode      	:Normal</p> <p>Exposure difference  	:N/A</p> <p>Flexible program     	:N/A</p> <p>Sensitivity          	:Auto</p> <p>Sharpening           	:Auto</p> <p>Image Type           	:Color</p> <p>Color Mode           	:N/A</p> <p>Hue adjustment       	:N/A</p> <p>Saturation Control   	:N/A</p> <p>Tone compensation    	:Normal</p> <p>Latitude(GPS)        	:N/A</p> <p>Longitude(GPS)       	:N/A</p> <p>Altitude(GPS)        	:N/A One of the things I try to do when I’m working on my csharp projects is to make sure I don’t have any compile errors or warnings.  The reason for removing the errors is probably obvious–if you have an error, your code isn’t going to run at that place.  But what about the warnings?

Well, warnings are there because they are potential errors, things you really ought to fix because they could end up causing unexpected results in your program.

So how do we deal with warnings?

The first thing you should do in your code is go through all of your warnings and make sure they aren’t something you couldn’t fix by rearranging your code.  Most of the warnings that show up in your code are of this type.

But what about warnings that you can’t remove?

This code

int _x = 1;
string s=null;
if(_x is object)
    s = "abc";
if (_x != 1)
    s = "xyz";
MessageBox.Show(s);

Will produce an error at “if(_x is object)” because value types are always objects.  There is no need to have the if statement.

We should remove the conditional statement or replace it with something more meaningful.  But let’s suppose that we can’t.  How can we prevent the error from showing up?

By using the #pragma warning directive.

            int _x = 1;
            string s=null;
#pragma warning disable 183
            if(_x is object)
                s = "abc";
#pragma warning restore 183
            if (_x != 1)
                s = "xyz";
            MessageBox.Show(s);

The disable statement will disable a comma-separated list of warnings from showing up in your compiler’s warning list.  The restore statement turns them back on.

Unfortunately, the place where I need this feature the most is in some VB.NET code that I’m writing.  Currently this feature is not supported in VB.NET, but there is a rumor that it is to be supported in the 2010 release.

Related Post

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail

Bear