String.Replace not working???

G

Guest

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
 
G

Guest

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
 
M

Marina

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.
 
C

Cor Ligthert [MVP]

BBM,

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

I hope this helps

Cor
 
J

Jay B. Harlow [MVP - Outlook]

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

| 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
 
D

Dick Grier

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.
 
G

Guest

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
 
C

Cor Ligthert [MVP]

BBM,

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

Cor
 
A

Armin Zingler

BBM said:
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
 
J

Jay B. Harlow [MVP - Outlook]

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



| 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.
| >
| > | > > 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
| >
| >
| >
 
J

Jay B. Harlow [MVP - Outlook]

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

| 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.
| >
| > | > > 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
| >
| >
| >
 
C

Cor Ligthert [MVP]

Jay,

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

That should be at least a distinct.

Cor
 
T

Tym

It is rumoured that on Thu, 4 Aug 2005 10:26:15 -0500, "Jay B. Harlow
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!!
 
J

Jay B. Harlow [MVP - Outlook]

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

| Jay,
|
| In my opinion is stringbuilder not a value however an object.
|
| That should be at least a distinct.
|
| Cor
|
|
 
C

Cor Ligthert [MVP]

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
 
G

Guest

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
 
J

Jay B. Harlow [MVP - Outlook]

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

| 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
|
|
|
 

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