The Daily Parker

Politics, Weather, Photography, and the Dog

Compiler warnings

Read, understand, and then fix your compiler warnings.

Compiler warnings let you know that you've either done something wrong, or you've done something non-standard. Either way, ignorning compiler warnings shows a lack of discipline and skill; it's something like ignoring big red "warning" signs in real life.

I'm working on a .NET solution that, when last compiled, generated over 60 warning messages. A couple of them I put in to let other developers know about problems I found, but most warned about things that actually needed to get fixed.

For example, the following line of code:

if (comboBox.SelectedItem == "Do stuff")

generated the warning, "Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string.'"

In other words, the if statement was evaluating whether the object comboBox.SelectedItem was the same object as the string "Do Stuff," which is an impossibility. So the comparison would always fail, making it look like the feature was failing. Yet the compiler warned the developer about the problem, and even said how to fix it.

If you're wondering, the corrected line looks like this:

if ((string)comboBox.SelectedItem == "Do stuff")
Comments are closed