473,836 Members | 1,572 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
16 6180
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" <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 #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,
StringBuilde r!


--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******** ********@TK2MSF TNGP09.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 "informatio nal" and
can be safely ignored (such as StringBuilder.R eplace).

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.Fil l 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.R eplace would be set to None as it would be safe to
ignore the return value.

DataAdapter.Fil l 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******** ******@TK2MSFTN GP09.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
17817
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
62125
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
4515
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
2160
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
9163
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
1601
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
2446
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
9085
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
18646
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
9668
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
10546
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...
1
10588
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10254
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
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...
0
5823
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3112
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.