FxCop and String Creation Warning

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Charles Law

    #1

    FxCop and String Creation Warning

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

    <code>
    Select Case e.Tool.Key.ToUp per
    Case "FirstOption".T oUpper
    ' 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


  • Herfried K. Wagner [MVP]

    #2
    Re: FxCop and String Creation Warning

    "Charles Law" <blank@nowhere. com> schrieb:[color=blue]
    > 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.[/color]

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

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • Charles Law

      #3
      Re: FxCop and String Creation Warning

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

      Whereas

      Case "FirstOption".T oUpper

      is the same as

      Case "FIRSTOPTION".T oUpper

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

      Charles


      "Ray Cassick" <rcassick@nospa m.enterprocity. com> wrote in message
      news:%23uDQM$jV FHA.2520@TK2MSF TNGP09.phx.gbl. ..[color=blue]
      > No... there sno need to make the text dependent on the case of the
      > stirng....
      >
      > <code>
      > Select Case e.Tool.Key.ToUp per
      > Case "FIRSTOPTIO N"
      > ' Do stuff
      >
      > Case "SECONDOPTI ON"
      > ' Do other stuff
      >
      > Case Else
      > ' Ignore anything else
      >
      > End Select
      > </code>
      >
      >
      >
      >
      > "Charles Law" <blank@nowhere. com> wrote in message
      > news:el2ntQjVFH A.2960@TK2MSFTN GP15.phx.gbl...[color=green]
      >> Hi Mattias
      >>
      >> I could write it as all uppercase, but I prefer to see it as mixed case
      >> as[/color]
      > I[color=green]
      >> find it more readable. Also, the test then becomes dependent on the case[/color]
      > of[color=green]
      >> the string. Ideally, there would be a way of making Select ... Case[/color]
      > perform[color=green]
      >> a case insensitive test.
      >>
      >> Charles
      >>
      >>
      >> "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
      >> news:OzjDQmiVFH A.3620@TK2MSFTN GP09.phx.gbl...[color=darkred]
      >> > ><code>
      >> >> Select Case e.Tool.Key.ToUp per
      >> >> Case "FirstOption".T oUpper
      >> >> ' 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 "SECONDOPTI ON"
      >> >
      >> > instead? I'm not sure if that will get rid of the FxCop warnings
      >> > though.
      >> >
      >> >
      >> >
      >> > Mattias
      >> >
      >> > --
      >> > Mattias Sjögren [MVP] mattias @ mvps.org
      >> > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      >> > Please reply only to the newsgroup.[/color]
      >>
      >>[/color]
      >
      >[/color]


      Comment

      • Charles Law

        #4
        Re: FxCop and String Creation Warning

        Thanks as usual Herfried.

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

        Charles


        "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
        news:O44KSLkVFH A.2328@TK2MSFTN GP10.phx.gbl...[color=blue]
        > "Charles Law" <blank@nowhere. com> schrieb:[color=green]
        >> 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.[/color]
        >
        > Take a look at 'Option Compare {Text, Binary}'.
        >
        > --
        > M S Herfried K. Wagner
        > M V P <URL:http://dotnet.mvps.org/>
        > V B <URL:http://classicvb.org/petition/>[/color]


        Comment

        Working...