473,785 Members | 2,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 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
Nov 21 '05 #1
16 6165
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 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

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******** *************** ***********@mic rosoft.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

Nov 21 '05 #3
BBM,

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

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 ", "TResourcesHist ory")

StringBuilder.R eplace works as StringBuilder is mutable.

Hope this helps
Jay

"BBM" <bb*@bbmcompany .com> wrote in message
news:35******** *************** ***********@mic rosoft.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
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 ", "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.
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******** *************** ***********@mic rosoft.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


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.Ole Db.OleDbCommand .ExecuteNonQuer y: 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******** *************** ***********@mic rosoft.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******** *************** ***********@mic rosoft.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
| >
| >
| >
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
17815
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 ie. "hello world" will be come... "_ _ _ _ _ _ _ _ _ _" (ive added xtra spaces so it dosnt look like one line!) with the method i have written only the last char is replaced, loop problem i guess! public String hideWord(String word) {
4
62113
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 supposed to write this function? String.replace(/</g,'&lt;');
4
4510
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("&<"); string::size_type begIndex, endIndex;
9
2157
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, Y=156}{X=311.2285, Y=156}{X=356, Y=200.7715}{X=356, Y=256}{X=200, Y=150}{X=200, Y=177.6142}{X=177.6142, Y=200}{X=150, Y=200}{X=122.3858, Y=200}{X=100, Y=177.6142}{X=100, Y=150}{X=100, Y=122.3858}{X=122.3858, Y=100}{X=150, Y=100}{X=177.6142, Y=100}{X=200,...
9
9160
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 StringBuilder. At present I am porting a VB6 webclass app to VB.NET and therefore I am trying to make it as efficent as possible via the new functionality of VB.NET.
4
1595
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 alternative to this? Example: Property orgname() As String Get orgname = m_orgname End Get Set(ByVal Value As String)
5
2443
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 to replace TAB's with "&nbsp;"'s to preserve the user's indentation. My questions are: 1) even though I'll probably look it up before I get a reply.... whats the vb code for TAB character? 2) more importantly; do I have to run this large field...
1
9074
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 string.replace(from_string, to_string, len(string)) to replace names in a file with their IP address. For example, I have definitions file, that looks something like: 10.1.3.4 LANDING_GEAR 20.11.222.4 ALTIMETER_100
10
18638
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 parameter with which you can write wanted character at that position. But no, Replace method only allows replacing one known character with another. The problem is I don't know the character to replace, just must replace the character at a known...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10152
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8974
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.