FxCop and String Creation Warning

C

Charles Law

I have just run FxCop and it reported a problem line 2 of the following:

<code>
Select Case e.Tool.Key.ToUpper
Case "FirstOption".ToUpper
' Do stuff

Case "SecondOption".ToUpper
' Do other stuff

Case Else
' Ignore anything else

End Select
</code>

It suggests that I should not create strings that are not subsequently
assigned to a variable, but to use String.Compare() instead.

Any thoughts on how one could do this and still use Select ... Case?

TIA

Charles
 
M

Mattias Sjögren

Select Case e.Tool.Key.ToUpper
Case "FirstOption".ToUpper
' Do stuff

Case "SecondOption".ToUpper
' Do other stuff

Case Else
' Ignore anything else

End Select
</code>

Why all the ToUpper calls? Why don't you simply do

Case "SECONDOPTION"

instead? I'm not sure if that will get rid of the FxCop warnings
though.



Mattias
 
C

Charles Law

Hi Mattias

I could write it as all uppercase, but I prefer to see it as mixed case as I
find it more readable. Also, the test then becomes dependent on the case of
the string. Ideally, there would be a way of making Select ... Case perform
a case insensitive test.

Charles
 
R

Ray Cassick

No... there sno need to make the text dependent on the case of the
stirng....

<code>
Select Case e.Tool.Key.ToUpper
Case "FIRSTOPTION"
' Do stuff

Case "SECONDOPTION"
' Do other stuff

Case Else
' Ignore anything else

End Select
</code>




Charles Law said:
Hi Mattias

I could write it as all uppercase, but I prefer to see it as mixed case as I
find it more readable. Also, the test then becomes dependent on the case of
the string. Ideally, there would be a way of making Select ... Case perform
a case insensitive test.

Charles
 
H

Herfried K. Wagner [MVP]

Charles Law said:
I could write it as all uppercase, but I prefer to see it as mixed case as
I find it more readable. Also, the test then becomes dependent on the case
of the string. Ideally, there would be a way of making Select ... Case
perform a case insensitive test.

Take a look at 'Option Compare {Text, Binary}'.
 
C

Charles Law

What I mean is that it is dependent on me typing FirstOption in uppercase:
FIRSTOPTION.

Whereas

Case "FirstOption".ToUpper

is the same as

Case "FIRSTOPTION".ToUpper

i.e. it doesn't matter what case the string is in.

Charles


Ray Cassick said:
No... there sno need to make the text dependent on the case of the
stirng....

<code>
Select Case e.Tool.Key.ToUpper
Case "FIRSTOPTION"
' Do stuff

Case "SECONDOPTION"
' Do other stuff

Case Else
' Ignore anything else

End Select
</code>
 
C

Charles Law

Thanks as usual Herfried.

They don't call me Mr Short-sighted, Doesn't Look Where He's Going for
nothing ;-)

Charles
 

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