473,404 Members | 2,179 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,404 software developers and data experts.

Strange thing using Replace(..) in vb.net

Hi,
in our application we use the following code snippet:

Dim target As String = Replace(txtLinkTarget.Text, "<SEP>", "'")
If target.StartsWith("(") AndAlso target.EndsWith(")") Then

In line 1 the text-value of of a textbox is retrieved. All occurences of
<SEP> are then replaced by an apostrophe.

However, if txtLinkTarget.Text returns an empty string, the Replace
function evaluates to Nothing, giving me a nice exception in line 2.
How is that possible?

Jul 21 '05 #1
5 1429
> How is that possible?

The Replace method has two Exceptions:

ArgumentNullException = original string is a null reference (Nothing in
Visual Basic).
ArgumentException = original string is an empty string ("").

http://msdn.microsoft.com/library/de...placeTopic.asp

You need to verify that your original string is not null (Nothing) or empty
("") before calling replace.

If (Not OriginalString Is Nothing) AndAlso (Not OriginalString = "") Then

'Perform your replace and StartsWith/EndsWith here...

End If

Paul

Jul 21 '05 #2
Reik,

I thought that all your answers are here.

http://msdn.microsoft.com/library/de...swithtopic.asp

In VBNet is a
dim a as string = nothing
not the same as
dim a as string = ""
The last is a string empty.

I hope this helps?

Cor
Jul 21 '05 #3
Havagan,

You reference to the String.Replace while in my opinion the VB Replace is
used.

Cor
Jul 21 '05 #4
Thanks Paul

This is somewhat inconvinient, adding two additional lines to check if the
string is correct. What harm could an empty string do in the Replace
function. The function should be able to handle empty strings just like any
string object other than nothing.
"Havagan" wrote:
How is that possible?


The Replace method has two Exceptions:

ArgumentNullException = original string is a null reference (Nothing in
Visual Basic).
ArgumentException = original string is an empty string ("").

http://msdn.microsoft.com/library/de...placeTopic.asp

You need to verify that your original string is not null (Nothing) or empty
("") before calling replace.

If (Not OriginalString Is Nothing) AndAlso (Not OriginalString = "") Then

'Perform your replace and StartsWith/EndsWith here...

End If

Paul

Jul 21 '05 #5
Oh I thougth Havagan meant VB Replace, but I think its the same problem.

"Cor Ligthert" wrote:
Havagan,

You reference to the String.Replace while in my opinion the VB Replace is
used.

Cor

Jul 21 '05 #6

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

Similar topics

14
by: Allcomp | last post by:
Hello, I have seen something really strange in VB6 If I do a Int ( (5 * 1.2)) , I receive the value 5, but I should receive 6? Is this a bug or something really "normal". I can see that if I...
11
by: F. Da Costa | last post by:
Hi, This is a strange issue I'v been staring at for half a day now. It concerns catching keys via the onkeydown handler. In IE5+ it works fine but in Moz 1.6 (& Firebird 0.7+) it behaves most...
24
by: David | last post by:
hello. when doing the simple following computation, the value put into the variable numMinusOne is NOT the same as what the computation is showed to be in the Watch window!! here is the code:...
9
by: Abby Lee | last post by:
http://www.ncsa.uiuc.edu/Divisions/Admin/reimb.asp There is just too much code to place here. onChange you trigger a function to add the numbers in the the column you entered an amount in...
8
by: grundmann | last post by:
Hello, i got a strange compiler error. When compiling the following: // forward declarations typedef AvlTree<LineSegment,LineSegmentComperator> LSTree; void handleEventPoint (const...
5
by: Reik | last post by:
Hi, in our application we use the following code snippet: Dim target As String = Replace(txtLinkTarget.Text, "<SEP>", "'") If target.StartsWith("(") AndAlso target.EndsWith(")") Then In line...
2
by: Stimp | last post by:
I have a function to replace the strange quotes (Chr(8220) and Chr(8221)) with standard quotes... e.g. Public Shared Function TextFormat(ByVal InputString As String) As String InputString =...
1
by: kjensen | last post by:
Hi I'm working on a simple FTP server implementation but I've run into a strange problem. I use this function to recieve: def stor(self): f = open('test', 'w') while 1:
112
by: Prisoner at War | last post by:
Friends, your opinions and advice, please: I have a very simple JavaScript image-swap which works on my end but when uploaded to my host at http://buildit.sitesell.com/sunnyside.html does not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
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...

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.