String.Replace not working???

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

    #1

    String.Replace not working???

    This is so bizarre I hesitate to post it, but I need to get this working.

    My code looks like this...

    Private Const cmdTestResource sSel = "SELECT * FROM TResources" & _
    " WHERE Scenario =
    @Scenario"
    Dim cmdstr as String

    cmdstr = cmdTestResource sSel
    If history Then
    cmdstr.Replace( "TResources ", "TResourcesHist ory")
    End If
    Dim cmdS As SqlCommand = New SqlCommand(cmds tr, cnTest)

    History is true. Coming out of the "if" statement, cmdstr is unchanged.
    CommandText of the SQLCommand contains "TResources " not "TResourcesHist ory"
    This same code template works in the same program in about a dozen other
    places. No exceptions thrown. I can do the replace on cmdstr in the
    command window while debugging the app and it works fine.

    What's up with this???

    Thanks for your help.

    BBM
  • BBM

    #2
    RE: String.Replace not working???

    Hi everyone,

    I got this to work by changing cmdstr to a StringBuilder object, but I don't
    know why I had to do this. If anyone can shed some light here I'd appreciate
    it.

    BBM

    "BBM" wrote:
    [color=blue]
    > This is so bizarre I hesitate to post it, but I need to get this working.
    >
    > My code looks like this...
    >
    > Private Const cmdTestResource sSel = "SELECT * FROM TResources" & _
    > " WHERE Scenario =
    > @Scenario"
    > Dim cmdstr as String
    >
    > cmdstr = cmdTestResource sSel
    > If history Then
    > cmdstr.Replace( "TResources ", "TResourcesHist ory")
    > End If
    > Dim cmdS As SqlCommand = New SqlCommand(cmds tr, cnTest)
    >
    > History is true. Coming out of the "if" statement, cmdstr is unchanged.
    > CommandText of the SQLCommand contains "TResources " not "TResourcesHist ory"
    > This same code template works in the same program in about a dozen other
    > places. No exceptions thrown. I can do the replace on cmdstr in the
    > command window while debugging the app and it works fine.
    >
    > What's up with this???
    >
    > Thanks for your help.
    >
    > BBM[/color]

    Comment

    • Marina

      #3
      Re: String.Replace not working???

      Replace does not modify the original string. It returns a new one with the
      replacements.

      Meaning, you have to assign the return of Replace to a variable. This is
      well documented.

      "BBM" <bbm@bbmcompany .com> wrote in message
      news:35227207-321B-4AD0-B6F9-992E68624B14@mi crosoft.com...[color=blue]
      > This is so bizarre I hesitate to post it, but I need to get this working.
      >
      > My code looks like this...
      >
      > Private Const cmdTestResource sSel = "SELECT * FROM TResources" & _
      > " WHERE Scenario =
      > @Scenario"
      > Dim cmdstr as String
      >
      > cmdstr = cmdTestResource sSel
      > If history Then
      > cmdstr.Replace( "TResources ", "TResourcesHist ory")
      > End If
      > Dim cmdS As SqlCommand = New SqlCommand(cmds tr, cnTest)
      >
      > History is true. Coming out of the "if" statement, cmdstr is unchanged.
      > CommandText of the SQLCommand contains "TResources " not
      > "TResourcesHist ory"
      > This same code template works in the same program in about a dozen other
      > places. No exceptions thrown. I can do the replace on cmdstr in the
      > command window while debugging the app and it works fine.
      >
      > What's up with this???
      >
      > Thanks for your help.
      >
      > BBM[/color]


      Comment

      • Cor Ligthert [MVP]

        #4
        Re: String.Replace not working???

        BBM,

        dim myresultstring as string = cmdstr.Replace( "TResources ",
        "TResourcesHist ory")
        or even
        cmdstr = cmdstr.Replace( "TResources ", "TResourcesHist ory")

        I hope this helps

        Cor


        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: String.Replace not working???

          BBM,
          String.Replace is a Function, as String is immutable. It returns the
          modified string.

          Try something like:

          cmdstr = cmdstr.Replace( "TResources ", "TResourcesHist ory")

          StringBuilder.R eplace works as StringBuilder is mutable.

          Hope this helps
          Jay

          "BBM" <bbm@bbmcompany .com> wrote in message
          news:35227207-321B-4AD0-B6F9-992E68624B14@mi crosoft.com...
          | This is so bizarre I hesitate to post it, but I need to get this working.
          |
          | My code looks like this...
          |
          | Private Const cmdTestResource sSel = "SELECT * FROM TResources" & _
          | " WHERE Scenario =
          | @Scenario"
          | Dim cmdstr as String
          |
          | cmdstr = cmdTestResource sSel
          | If history Then
          | cmdstr.Replace( "TResources ", "TResourcesHist ory")
          | End If
          | Dim cmdS As SqlCommand = New SqlCommand(cmds tr, cnTest)
          |
          | History is true. Coming out of the "if" statement, cmdstr is unchanged.
          | CommandText of the SQLCommand contains "TResources " not
          "TResourcesHist ory"
          | This same code template works in the same program in about a dozen other
          | places. No exceptions thrown. I can do the replace on cmdstr in the
          | command window while debugging the app and it works fine.
          |
          | What's up with this???
          |
          | Thanks for your help.
          |
          | BBM


          Comment

          • Dick Grier

            #6
            Re: String.Replace not working???

            Hi,

            This is a little strange (I hadn't tried it before). However, the follow
            (old-style VB) does work:

            Replace(cmdstr, "TResources ", "TResourcesHist ory")

            Dick
            --
            Richard Grier (Microsoft Visual Basic MVP)

            See www.hardandsoftware.net for contact information.

            Author of Visual Basic Programmer's Guide to Serial Communications, 4th
            Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
            www.mabry.com/vbpgser4 to order.


            Comment

            • BBM

              #7
              Re: String.Replace not working???

              You're right. I was in a hurry and missed the fact that string.replace is a
              function.

              However, I've never understood why VB.Net allows you to do this (use a
              function without requiring you assign it to something). In 1986 Turbo Pascal
              could catch this. There's no reason for something to be a function unless
              the return value is useful.

              I was lucky enough to catch my error in test. I'll bet there's about a
              billion occurences of errors similar to mine in production code.

              Thanks again for your help and prompt response.

              BBM

              "Marina" wrote:
              [color=blue]
              > Replace does not modify the original string. It returns a new one with the
              > replacements.
              >
              > Meaning, you have to assign the return of Replace to a variable. This is
              > well documented.
              >
              > "BBM" <bbm@bbmcompany .com> wrote in message
              > news:35227207-321B-4AD0-B6F9-992E68624B14@mi crosoft.com...[color=green]
              > > This is so bizarre I hesitate to post it, but I need to get this working.
              > >
              > > My code looks like this...
              > >
              > > Private Const cmdTestResource sSel = "SELECT * FROM TResources" & _
              > > " WHERE Scenario =
              > > @Scenario"
              > > Dim cmdstr as String
              > >
              > > cmdstr = cmdTestResource sSel
              > > If history Then
              > > cmdstr.Replace( "TResources ", "TResourcesHist ory")
              > > End If
              > > Dim cmdS As SqlCommand = New SqlCommand(cmds tr, cnTest)
              > >
              > > History is true. Coming out of the "if" statement, cmdstr is unchanged.
              > > CommandText of the SQLCommand contains "TResources " not
              > > "TResourcesHist ory"
              > > This same code template works in the same program in about a dozen other
              > > places. No exceptions thrown. I can do the replace on cmdstr in the
              > > command window while debugging the app and it works fine.
              > >
              > > What's up with this???
              > >
              > > Thanks for your help.
              > >
              > > BBM[/color]
              >
              >
              >[/color]

              Comment

              • Cor Ligthert [MVP]

                #8
                Re: String.Replace not working???

                BBM,

                This newsgroup is full of complaints that this is possible expressly by
                Herfried.

                Cor


                Comment

                • Armin Zingler

                  #9
                  Re: String.Replace not working???

                  "BBM" <bbm@bbmcompany .com> schrieb[color=blue]
                  > You're right. I was in a hurry and missed the fact that
                  > string.replace is a function.
                  >
                  > However, I've never understood why VB.Net allows you to do this (use
                  > a function without requiring you assign it to something). In 1986
                  > Turbo Pascal could catch this. There's no reason for something to
                  > be a function unless the return value is useful.[/color]



                  No reason for the function, but a reason for the caller. Far back in VB6
                  there was a function called "Doevents" returning the number of open Forms.
                  ;-) I don't need the number, thus I ignored the return value. Not each
                  function's *only* purpose is returning a value. Or look at
                  System.Data.Ole Db.OleDbCommand .ExecuteNonQuer y: I'm not always interested in
                  the number of records affected.


                  Armin

                  Comment

                  • Jay B. Harlow [MVP - Outlook]

                    #10
                    Re: String.Replace not working???

                    BBM,
                    | However, I've never understood why VB.Net allows you to do this (use a
                    | function without requiring you assign it to something). In 1986 Turbo
                    Pascal
                    | could catch this. There's no reason for something to be a function unless
                    | the return value is useful.
                    I agree; It would be nice if it was an option, however for those developers
                    who create functions that return an "error status" (rather then relying on
                    Exceptions) & never check them. Or those developers who don't really know
                    the difference between Sub & Function, who always use Function. It could
                    "break" their code. Or those "advanced developers" who create functions that
                    return "additional info" that is needed sometimes & not needed other times.
                    It could be problematic for their code. Of course they could simply assign
                    the return value to a local value & ignore. Wait, that's what the compiler
                    is doing now ;-)

                    ' Return True it succeeded, False it Fails
                    Public Function DoSomething() As Boolean
                    Try
                    Return True
                    Catch
                    Return False
                    End Try
                    End Function


                    ' I don't care if I did something, ignore return code
                    DoSomething()

                    verses
                    Dim bitBucket As Boolean = DoSomething()
                    ' Never refer to bitBucket again

                    VS.NET 2005 adds some new compiler messages that can be configured as either
                    errors or warnings, I'm not sure if not using the return value is one of
                    them (I don't have my VS.NET 2005 VPC running right now to check)... One of
                    the messages I know is there is unused local variable, unfortunately in the
                    above sample bitBucket would be marked as unused...

                    Unfortunately I suspect its too late in the VS.NET 2005 development cycle to
                    add the message.

                    | I was lucky enough to catch my error in test. I'll bet there's about a
                    | billion occurences of errors similar to mine in production code.
                    I would suggest Unit Testing (TDD - Test Driven Development) to help ensure
                    bugs like this do not make it to your production code. www.nunit.org is unit
                    testing tool I currently use. VS.NET 2005's Team System also includes a unit
                    testing tool.

                    Hope this helps
                    Jay



                    "BBM" <bbm@bbmcompany .com> wrote in message
                    news:7CAAE8F1-FFB5-4141-8478-6090E0E3D237@mi crosoft.com...
                    | You're right. I was in a hurry and missed the fact that string.replace is
                    a
                    | function.
                    |
                    | However, I've never understood why VB.Net allows you to do this (use a
                    | function without requiring you assign it to something). In 1986 Turbo
                    Pascal
                    | could catch this. There's no reason for something to be a function unless
                    | the return value is useful.
                    |
                    | I was lucky enough to catch my error in test. I'll bet there's about a
                    | billion occurences of errors similar to mine in production code.
                    |
                    | Thanks again for your help and prompt response.
                    |
                    | BBM
                    |
                    | "Marina" wrote:
                    |
                    | > Replace does not modify the original string. It returns a new one with
                    the
                    | > replacements.
                    | >
                    | > Meaning, you have to assign the return of Replace to a variable. This is
                    | > well documented.
                    | >
                    | > "BBM" <bbm@bbmcompany .com> wrote in message
                    | > news:35227207-321B-4AD0-B6F9-992E68624B14@mi crosoft.com...
                    | > > This is so bizarre I hesitate to post it, but I need to get this
                    working.
                    | > >
                    | > > My code looks like this...
                    | > >
                    | > > Private Const cmdTestResource sSel = "SELECT * FROM TResources" & _
                    | > > " WHERE Scenario =
                    | > > @Scenario"
                    | > > Dim cmdstr as String
                    | > >
                    | > > cmdstr = cmdTestResource sSel
                    | > > If history Then
                    | > > cmdstr.Replace( "TResources ", "TResourcesHist ory")
                    | > > End If
                    | > > Dim cmdS As SqlCommand = New SqlCommand(cmds tr, cnTest)
                    | > >
                    | > > History is true. Coming out of the "if" statement, cmdstr is
                    unchanged.
                    | > > CommandText of the SQLCommand contains "TResources " not
                    | > > "TResourcesHist ory"
                    | > > This same code template works in the same program in about a dozen
                    other
                    | > > places. No exceptions thrown. I can do the replace on cmdstr in the
                    | > > command window while debugging the app and it works fine.
                    | > >
                    | > > What's up with this???
                    | > >
                    | > > Thanks for your help.
                    | > >
                    | > > BBM
                    | >
                    | >
                    | >


                    Comment

                    • Jay B. Harlow [MVP - Outlook]

                      #11
                      Re: String.Replace not working???

                      BBM,
                      StringBuilder!

                      StringBuilder is an example where it would be problematic.

                      All the methods on StringBuilder return "me" so as you can chain calls to
                      the methods, such as:

                      Dim sb As New System.Text.Str ingBuilder("ABA ABBAA")

                      sb.Replace("A", "0").Replace("B ", "1")

                      Or most people simply use:

                      sb.Replace("A", "0")
                      sb.Replace("B", "1")

                      If there was a warning or error, all the StringBuilder methods would have a
                      problem.

                      I can see "builders" returning "me" from all the methods so that you can
                      chain them as above.

                      Hope this helps
                      Jay

                      "BBM" <bbm@bbmcompany .com> wrote in message
                      news:7CAAE8F1-FFB5-4141-8478-6090E0E3D237@mi crosoft.com...
                      | You're right. I was in a hurry and missed the fact that string.replace is
                      a
                      | function.
                      |
                      | However, I've never understood why VB.Net allows you to do this (use a
                      | function without requiring you assign it to something). In 1986 Turbo
                      Pascal
                      | could catch this. There's no reason for something to be a function unless
                      | the return value is useful.
                      |
                      | I was lucky enough to catch my error in test. I'll bet there's about a
                      | billion occurences of errors similar to mine in production code.
                      |
                      | Thanks again for your help and prompt response.
                      |
                      | BBM
                      |
                      | "Marina" wrote:
                      |
                      | > Replace does not modify the original string. It returns a new one with
                      the
                      | > replacements.
                      | >
                      | > Meaning, you have to assign the return of Replace to a variable. This is
                      | > well documented.
                      | >
                      | > "BBM" <bbm@bbmcompany .com> wrote in message
                      | > news:35227207-321B-4AD0-B6F9-992E68624B14@mi crosoft.com...
                      | > > This is so bizarre I hesitate to post it, but I need to get this
                      working.
                      | > >
                      | > > My code looks like this...
                      | > >
                      | > > Private Const cmdTestResource sSel = "SELECT * FROM TResources" & _
                      | > > " WHERE Scenario =
                      | > > @Scenario"
                      | > > Dim cmdstr as String
                      | > >
                      | > > cmdstr = cmdTestResource sSel
                      | > > If history Then
                      | > > cmdstr.Replace( "TResources ", "TResourcesHist ory")
                      | > > End If
                      | > > Dim cmdS As SqlCommand = New SqlCommand(cmds tr, cnTest)
                      | > >
                      | > > History is true. Coming out of the "if" statement, cmdstr is
                      unchanged.
                      | > > CommandText of the SQLCommand contains "TResources " not
                      | > > "TResourcesHist ory"
                      | > > This same code template works in the same program in about a dozen
                      other
                      | > > places. No exceptions thrown. I can do the replace on cmdstr in the
                      | > > command window while debugging the app and it works fine.
                      | > >
                      | > > What's up with this???
                      | > >
                      | > > Thanks for your help.
                      | > >
                      | > > BBM
                      | >
                      | >
                      | >


                      Comment

                      • Cor Ligthert [MVP]

                        #12
                        Re: String.Replace not working???

                        Jay,

                        In my opinion is stringbuilder not a value however an object.

                        That should be at least a distinct.

                        Cor


                        Comment

                        • Tym

                          #13
                          Re: String.Replace not working???

                          It is rumoured that on Thu, 4 Aug 2005 10:26:15 -0500, "Jay B. Harlow
                          [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> spake word of wisdom thus:
                          [color=blue]
                          >BBM,
                          >StringBuilde r![/color]

                          --8<----8<----8<----8<----8<----8<----8<----8<--

                          Ew!!!! Top posting!! :-P
                          ---


                          Tym

                          Please do not adjust your brain, there is a fault with reality

                          Email - Domain is valid - user is not - I'm sure you can work it out though!

                          However, there is an agressive spam trap here, so you'll get blacklisted until I
                          white list you. If you've emailed and asked for a reply and not got one - please
                          post a message in here and I'll re-check the black hole!

                          A plague of Jonathan Ross a thousand fold on the spammers!!

                          Comment

                          • Jay B. Harlow [MVP - Outlook]

                            #14
                            Re: String.Replace not working???

                            Cor,
                            String is also an object by virtue String is a Reference Type.

                            How are you defining "value" verses defining "object"?

                            Based on most ways I would define value & object, I'm not seeing why being a
                            value verses being an object would make a real difference here!

                            Double.TryParse for example, returns False if it was not able to convert the
                            number, according to the documentation it also sets the result to Zero if it
                            was not able to convert the number. Simply having the result set to Zero may
                            be sufficient for my algorithm to work without worrying that TryParse
                            returned True or False...

                            For example, I might call it this way:

                            Dim result As Double
                            Double.TryParse ("x", ..., result)

                            As my requirements allow setting result = 0 for invalid input as acceptable,
                            although I would expect Double.Nan would be a more logical default for
                            invalid input.

                            Interesting, it appears that Double.TryParse in .NET 1.1 does not set the
                            result to zero if it failed, it appears leave the value as is, in which case
                            I could code it as:

                            Dim result As Double = Double.Nan
                            Double.TryParse ("x", ..., result)

                            Hope this helps
                            Jay

                            "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> wrote in message
                            news:%236PttuQm FHA.3120@TK2MSF TNGP09.phx.gbl. ..
                            | Jay,
                            |
                            | In my opinion is stringbuilder not a value however an object.
                            |
                            | That should be at least a distinct.
                            |
                            | Cor
                            |
                            |


                            Comment

                            • Cor Ligthert [MVP]

                              #15
                              Re: String.Replace not working???

                              Jay,
                              [color=blue]
                              > String is also an object by virtue String is a Reference Type.
                              >[/color]

                              I was almost imidiatly making a message when I had sent it.

                              Because I was expecting this answer from you

                              :-)

                              However I decided to wait to make it easier to reply.

                              The string has so much special ways it is handled in VBNet and is handled
                              that way, that I in my opinion it is worth to handle it special in the IDE
                              in this.

                              Only alone the many problems we have seen in this in this newsgroup would
                              justify that. It is not changing the behaviour it is adding a warning.

                              Cor



                              Comment

                              Working...