469,273 Members | 1,810 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,273 developers. It's quick & easy.

String.Replace not working???

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

My code looks like this...

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

cmdstr = cmdTestResourcesSel
If history Then
cmdstr.Replace("TResources", "TResourcesHistory")
End If
Dim cmdS As SqlCommand = New SqlCommand(cmdstr, cnTest)

History is true. Coming out of the "if" statement, cmdstr is unchanged.
CommandText of the SQLCommand contains "TResources" not "TResourcesHistory"
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
Nov 21 '05 #1
16 5502
BBM
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:
This is so bizarre I hesitate to post it, but I need to get this working.

My code looks like this...

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

cmdstr = cmdTestResourcesSel
If history Then
cmdstr.Replace("TResources", "TResourcesHistory")
End If
Dim cmdS As SqlCommand = New SqlCommand(cmdstr, cnTest)

History is true. Coming out of the "if" statement, cmdstr is unchanged.
CommandText of the SQLCommand contains "TResources" not "TResourcesHistory"
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

Nov 21 '05 #2
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" <bb*@bbmcompany.com> wrote in message
news:35**********************************@microsof t.com...
This is so bizarre I hesitate to post it, but I need to get this working.

My code looks like this...

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

cmdstr = cmdTestResourcesSel
If history Then
cmdstr.Replace("TResources", "TResourcesHistory")
End If
Dim cmdS As SqlCommand = New SqlCommand(cmdstr, cnTest)

History is true. Coming out of the "if" statement, cmdstr is unchanged.
CommandText of the SQLCommand contains "TResources" not
"TResourcesHistory"
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

Nov 21 '05 #3
BBM,

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

I hope this helps

Cor
Nov 21 '05 #4
BBM,
String.Replace is a Function, as String is immutable. It returns the
modified string.

Try something like:

cmdstr = cmdstr.Replace("TResources", "TResourcesHistory")

StringBuilder.Replace works as StringBuilder is mutable.

Hope this helps
Jay

"BBM" <bb*@bbmcompany.com> wrote in message
news:35**********************************@microsof t.com...
| This is so bizarre I hesitate to post it, but I need to get this working.
|
| My code looks like this...
|
| Private Const cmdTestResourcesSel = "SELECT * FROM TResources" & _
| " WHERE Scenario =
| @Scenario"
| Dim cmdstr as String
|
| cmdstr = cmdTestResourcesSel
| If history Then
| cmdstr.Replace("TResources", "TResourcesHistory")
| End If
| Dim cmdS As SqlCommand = New SqlCommand(cmdstr, cnTest)
|
| History is true. Coming out of the "if" statement, cmdstr is unchanged.
| CommandText of the SQLCommand contains "TResources" not
"TResourcesHistory"
| 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
Nov 21 '05 #5
Hi,

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

Replace(cmdstr, "TResources", "TResourcesHistory")

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.
Nov 21 '05 #6
BBM
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" <bb*@bbmcompany.com> wrote in message
news:35**********************************@microsof t.com...
This is so bizarre I hesitate to post it, but I need to get this working.

My code looks like this...

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

cmdstr = cmdTestResourcesSel
If history Then
cmdstr.Replace("TResources", "TResourcesHistory")
End If
Dim cmdS As SqlCommand = New SqlCommand(cmdstr, cnTest)

History is true. Coming out of the "if" statement, cmdstr is unchanged.
CommandText of the SQLCommand contains "TResources" not
"TResourcesHistory"
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


Nov 21 '05 #7
BBM,

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

Cor
Nov 21 '05 #8
"BBM" <bb*@bbmcompany.com> schrieb
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.


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.OleDb.OleDbCommand.ExecuteNonQuery: I'm not always interested in
the number of records affected.
Armin

Nov 21 '05 #9
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" <bb*@bbmcompany.com> wrote in message
news:7C**********************************@microsof t.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" <bb*@bbmcompany.com> wrote in message
| > news:35**********************************@microsof t.com...
| > > This is so bizarre I hesitate to post it, but I need to get this
working.
| > >
| > > My code looks like this...
| > >
| > > Private Const cmdTestResourcesSel = "SELECT * FROM TResources" & _
| > > " WHERE Scenario =
| > > @Scenario"
| > > Dim cmdstr as String
| > >
| > > cmdstr = cmdTestResourcesSel
| > > If history Then
| > > cmdstr.Replace("TResources", "TResourcesHistory")
| > > End If
| > > Dim cmdS As SqlCommand = New SqlCommand(cmdstr, cnTest)
| > >
| > > History is true. Coming out of the "if" statement, cmdstr is
unchanged.
| > > CommandText of the SQLCommand contains "TResources" not
| > > "TResourcesHistory"
| > > 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
| >
| >
| >
Nov 21 '05 #10
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.StringBuilder("ABAABBAA")

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" <bb*@bbmcompany.com> wrote in message
news:7C**********************************@microsof t.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" <bb*@bbmcompany.com> wrote in message
| > news:35**********************************@microsof t.com...
| > > This is so bizarre I hesitate to post it, but I need to get this
working.
| > >
| > > My code looks like this...
| > >
| > > Private Const cmdTestResourcesSel = "SELECT * FROM TResources" & _
| > > " WHERE Scenario =
| > > @Scenario"
| > > Dim cmdstr as String
| > >
| > > cmdstr = cmdTestResourcesSel
| > > If history Then
| > > cmdstr.Replace("TResources", "TResourcesHistory")
| > > End If
| > > Dim cmdS As SqlCommand = New SqlCommand(cmdstr, cnTest)
| > >
| > > History is true. Coming out of the "if" statement, cmdstr is
unchanged.
| > > CommandText of the SQLCommand contains "TResources" not
| > > "TResourcesHistory"
| > > 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
| >
| >
| >
Nov 21 '05 #11
Jay,

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

That should be at least a distinct.

Cor
Nov 21 '05 #12
Tym
It is rumoured that on Thu, 4 Aug 2005 10:26:15 -0500, "Jay B. Harlow
[MVP - Outlook]" <Ja************@msn.com> spake word of wisdom thus:
BBM,
StringBuilder!


--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!!
Nov 21 '05 #13
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]" <no************@planet.nl> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
| Jay,
|
| In my opinion is stringbuilder not a value however an object.
|
| That should be at least a distinct.
|
| Cor
|
|
Nov 21 '05 #14
Jay,
String is also an object by virtue String is a Reference Type.


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

Nov 21 '05 #15
BBM
Add me to the list. Functions exist to return values. Having the language
allow cmdstr.replace("X", "Y") is the equivalent of allowing "+=2" as a valid
line (maybe this works? I haven't tried).

Thanks again

"Cor Ligthert [MVP]" wrote:
BBM,

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

Cor

Nov 21 '05 #16
Cor,
| 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.
Special case errors based on a variable being a String, IMHO is too special
case.

What happens when the next BBM calls function X on his class & nothing
happens, as function X on his class returns a new instance rather then
modifying the target object?

A more generalized approach (such as an Attribute) that would indicate that
the return value of a Function is "important" and should be dealt with (such
as String.Replace) or the return value of a Function is "informational" and
can be safely ignored (such as StringBuilder.Replace).

By "important" I mean that ignoring the return value may result in loss of
"data", as in the String.Replace case.

Of course then how do you categorize functions such as DataAdapter.Fill that
returns the number of rows effected. The above attribute might need to have
some form of Level such as None, Warn, Error...

Where StringBuilder.Replace would be set to None as it would be safe to
ignore the return value.

DataAdapter.Fill would be set to Warn, as the value can be important, but
not that important.

String.Replace would be set to Error, as the value is required & ignoring it
would result in "data loss".

I would probably treat no attribute as None.

Jay

Hope this helps
Jay

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:Ot**************@TK2MSFTNGP09.phx.gbl...
| Jay,
|
| > String is also an object by virtue String is a Reference Type.
| >
|
| 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
|
|
|
Nov 21 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by - ions | last post: by
4 posts views Thread by higabe | last post: by
9 posts views Thread by Crirus | last post: by
4 posts views Thread by Derek Martin | last post: by
5 posts views Thread by djc | last post: by
1 post views Thread by Michael Yanowitz | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.