473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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 6126
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: - ions | last post by:
Hi, i would like to know how to replace every char in a string with a certin given char using the String.replace(char oldChar,char newChar). I would like to replace all letters with an underscore...
4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
4
by: baobaoba | last post by:
Hi, I have a piece of code to do string replacement : #include <iostream> #include <string> using namespace std; void charEscape (string& src) { const string delims("&<");...
9
by: Crirus | last post by:
dim pp as string pp="{X=356, Y=256}{X=356, Y=311.2285}{X=311.2285, Y=356}{X=256, Y=356}{X=200.7715, Y=356}{X=156, Y=311.2285}{X=156, Y=256}{X=156, Y=200.7715}{X=200.7715, Y=156}{X=256,...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
4
by: Derek Martin | last post by:
I have an object with several string elements that I would like to check for invalid characters in the properties of each element. Can I use string.replace to do that or is there a better...
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
1
by: Michael Yanowitz | last post by:
Hello: I am hoping someone knows if there is an easier way to do this or someone already implemented something that does this, rather than reinventing the wheel: I have been using the...
10
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.