Home » Advanced CSharp » Removing Warnings from CSharp Compile Cycle

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.

 

Other post in Advanced CSharp

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Unsafe Mode in C#Unsafe Mode in C# One of the "advantages" of using CSharp instead of VB.NET is that if programmers want to, they have the option of bypassing the memory management of .NET and working with memory […]
  • CSharp fixed keywordCSharp fixed keyword Since I've already mentioned my bias against using unsafe mode in this post:Advanced CSharp - unsafe modeI'll skip my normal rant about that.  Just suffice it to say they don't […]
  • CSharp ?? OperatorCSharp ?? Operator One of the problems with learning a language early in its life cycle is that by the time version three comes out, you never look at or implement any of the new features. There are two […]
  • Delegates in .NETDelegates in .NET I received the following question:What is a delegate?  What problem does it solve?  and When might I use a delegate?A delegate is essentially a function pointer.  We have used […]
  • using – There’s more there than you are usingusing – There’s more there than you are using If you've spent more than a day programming in CSharp, you have already discovered the need for the using directive: using System; using System.Collections.Generic; using […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.