Visual Studio 2005 C# program, warnings

A

Alain Dekker

I'm trying to get VS 2005 to give me warnings on variables that are not
used, but I'm not being successful. Its a C# program. Its a debug build and
I've set the warning level in the project properties set to level "4".

I then add a local variable like this:
string szFred = string.Empty;

Then recompile...and nothing! No error or warning.

Why is this? Is this a bug in VS 2005 or am I missing something? I am not
overriding any warnings.

Thanks,
Alain
 
J

Jeff Johnson

I'm trying to get VS 2005 to give me warnings on variables that are not
used, but I'm not being successful. Its a C# program. Its a debug build
and I've set the warning level in the project properties set to level "4".

I then add a local variable like this:
string szFred = string.Empty;

Then recompile...and nothing! No error or warning.

Why is this? Is this a bug in VS 2005 or am I missing something? I am not
overriding any warnings.

As Bjørn said, you have actually USED the variable at this point. However,
you ought to be getting an error telling you that szFred* is never read
from.



*please oh PLEASE stop using Hungarian notation!
 
J

Jeff Johnson

Please, oh please stop calling something that's not Hungarian "Hungarian".

Strings in .NET aren't null-terminated, so "sz" isn't an appropriate tag
for string types.

Personally, I think it's a waste of time to harass people for their naming
conventions. But you should at least avoid mistaking an incorrect attempt
to use Hungarian for _actual_ Hungarian. Most of the complaints about
Hungarian come from the incorrect uses of Hungarian.

But that's the problem. 99.999% of people don't understand what Hungarian
notation was created for; they merely think it means "prepending a variable
name with an abbreviation that indicates the data type of the variable." So
I use the term the way they understand it.

And sometimes people do change their ways. I wrote VB for years and I
adhered strictly to this pseudo-Hungarian, even after learning it was the
bastardized form. But then .NET recommendations spoke against it clearly,
and I decided that since I was moving to a new Rome, I'd do as the new
Romans do. And I've come to like this new, non-decorated world, even to the
point of accepting camel casing (but still thinking it's butt-ugly).
 
A

Alain Dekker

This post is about C# warnings, not notation.

You're right, if I don't initialise the variable, I get a warning:
string Fred; // Generates warning
string Fred = string.Empty; // Generates *NO* warning, even at level 4

I think this is not the most helpful behaviour from the IDE. After more
research, I understand that MS wants us to use FxCop, but thats not ideal. I
have to install a very large Windows SDK, then install FxCop, then "analyse"
my .NET assemply in an external application, wade through tonnes of crap
about variable naming, hundreds of obscure messages, and then if I get lucky
I might find something useful like a variable isn't used.

Why can't the IDE have told me that like it used to [referring to VS
2003.NET and that old stalwart, VS6]?

Alain
 
A

Arne Vajhøj

This post is about C# warnings, not notation.

You're right, if I don't initialise the variable, I get a warning:
string Fred; // Generates warning
string Fred = string.Empty; // Generates *NO* warning, even at level 4

I think this is not the most helpful behaviour from the IDE. After more
research, I understand that MS wants us to use FxCop, but thats not ideal. I
have to install a very large Windows SDK, then install FxCop, then "analyse"
my .NET assemply in an external application, wade through tonnes of crap
about variable naming, hundreds of obscure messages, and then if I get lucky
I might find something useful like a variable isn't used.

Why can't the IDE have told me that like it used to [referring to VS
2003.NET and that old stalwart, VS6]?

If you can live with being 2 versions back, then a 1.35 of only
3.6 MB is available here:

http://archive.msdn.microsoft.com/codeanalysis/Release/ProjectReleases.aspx?ReleaseId=553

And maybe you should fix the other warnings as well!

:)

Arne
 
A

Alain Dekker

VS 2005 does *not* issue a warning for this C# line:
string Fred = string.Empty; // ...and never used

VS2005 *does* issue a warning for this C# line:
string Fred; // ...and never used

I've installed the latest SP1 and have warning level set to "4".

I'm not sure why you chose to flame me and basically call me an idiot for
using "obsolete tools". Thats neither helpful nor true. VS 2005 may not be
the latest, but its still supported and using by thousands of developers.
I'm just a guy who has been told to pick up a C# project a few weeks ago
when the previous developer suddenly resigned and left. I've never used C#
and VS 2005 before, so a bit more empathy and a bit less patronisation would
have been appreciated.

Alain
 
J

Jeff Johnson

VS 2005 does *not* issue a warning for this C# line:
string Fred = string.Empty; // ...and never used

VS2005 *does* issue a warning for this C# line:
string Fred; // ...and never used

I've installed the latest SP1 and have warning level set to "4".

Okay, I guess I've just been using VS 2008 99% of the time recently and have
gotten used to its behavior. I tried this in VS 2005 and Alain is indeed
correct: there is no compiler warning for the line with the assignment. Not
in the errors window, and not in the build output window.
 
A

Alain Dekker

Thanks for confirming I hadn't gone mad or missed some subtle setting
somewhere. I'm going to assume there's no fix for this (and furthermore that
MS are unlikely to do a patch or SP for this because VS 2005 is nearing the
end of its mainstream supported life), unless someone can help...

Thanks again,
Alain
 
A

Alain Dekker

OK, thanks. To be clear, I'm rapidly warming to both C# and .NET.

I just wanted to know why I wasn't seeing the warning. Seems a simple
question to me. Maybe there was some advanced setting in the IDE I could
change that would magically show it. Maybe someone could have said, "Hey,
thats a bug in VS 2005. Unfortunately Microsoft have declined to fix it.
There is FxCop v1.36 you could look at. It is fixed in later versions of the
IDE, but sorry I can't help more."

It turns out I was wrong - there isn't a setting I can turn on to show me
the warning. Its a plain 'ol common garden-variety bug in VS 2005. I was
also wrong about the simple answer. Sometimes a simple question doesn't get
a simple or friendly answer. Hey, maybe I even deserve to get my virtual ear
chewed, though in hindsight it seems unfair when I did report the bug
correctly [see post from Jeff Johnson]. Oh well, thats life.

Getting to quite like C# now. I just wrote my first delegate function and it
works nicely. But it is true that there are some niggles with VS 2005 that
are annoying. But hey, thats life too. I found VS 2003.NET particularly easy
to use coming from VS 6 for many years. The move from VS2003.NET to VS 2005
has not been as easy. I'd bet my house that neither VS 2008 or 2010 are
niggle or bug free. I'm sure they boast some great features (and bugfixes)
and I am looking forward to using them when the time comes...but for the
time being I'm stuck with VS 2005 and my Windows CE 5.0, CF v2.0 device and
I am not in a position to upgrade in the immediate future, no matter how
many tell me to dump my obsolete tool and get with the times.

For some developers working, say, on consumer or web applications only, its
just "get more memory here, upgrade this, upgrade that, get bigger hard disk
there". They forget that there are millions of users out there on older OSes
and older harware. They also forget that there are many thousands of real
programmers out there working on dedicated, single purpose devices that are
high value, low volume (I work in the medical devices and food inspection
fields). For us, upgrading is usually not an easy option,and will come at
considerable engineering / regulatory / engineering / training / and other
expense.

And there are those of us, who need a bit more justification for upgrading
than "its the latest". If you compare Word 97 to Word 2007, for example,
Word 97 beats Word 2007 hands down on performance. Having used both, frankly
the functionality that comes with 2007 just isn't of interest. My wife is a
professional author who uses Word 2007 every day. In her opinion, there is
only one feature where Word 2007 motivates her to use it in preference to
Word 97 or Word 2000, and thats improvements to the Track Changes
functionality. And if you're not too fussed or found the Track Changes
feature adequate in the previous versions, why would you upgrade? Seems to
me you'd be silly to. More money, slower program, new layout and features to
learn, etc. No brainer.

I say hats off to Microsoft for keeping Win32 alive and kicking to allow
older hardware and software to continue working, even well past their
official obsolescense dates.

Alain
 
A

Alain Dekker

Thanks for that answer, Pete, that does make a lot of sense. Kind of didn't
expect that. Not knowing C# very well, I would have assumed that these were
equivalent:

string Fred1 = string.Empty;
string Fred2 = "";

That they're not would explain it. Question now is...should we be using
'string.Empty' initialising to an empty string literal? Will start another
thread on that.

Thanks and regards,
Alain
 

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