String Usage (VB.Net)

  • Thread starter Thread starter Paul [Paradise Solutions]
  • Start date Start date
P

Paul [Paradise Solutions]

Hi all

I've often seen (and have quoted myself) the fact the using '=' to
update a strings content is processor expencive as it destroys the
string and then re-creates it. Some people say to used things like
insert and concat.
This is fine but Insert and Concat don't carry out the action on the
varibale specified - they return the result as a new string value that
needs to be assigned to a string varibale. So my question is, how are
you supposed to do this without using '='?

Any info apprieciated (especially on my last post about the task list...)

Paul
 
Strings are immutable so doing any sort of concatenation with them will use
resources. Ideally, you should use stringbuilders if you are doing anything
nontrivial.

There's no magic number that I've seen, but I think it's pretty much agreed
upon that if you have a string

Dim s as String

then add to it twice
s&="Something"
s&="SomethingElse"

It's not a big deal. HOwver, if I was iterating through a huge loop and
concatenating, it coudl be a huge deal. Stringbuilders have some overhead
associated with them as well, although it's well worth it if you are doing
any serious concatenation.

Another thing to remember is to declare strings as const as much as possible
to take advantage of interning.

HTH,

Bill
 
'&=' is the old-style VB way of string concat. It _may_ be better to use the
"new" style '+=' since no one knows when (if at all) the &= operator will be
deprecated.
 
As ever, this NGs a priceless stewing pot of minds - many thanks, one
and all!

Paul
 
If I remember correctly, back in VB6 days there was a significant and
oft-overlooked difference between operators "&" and "+".

Dim S as Variant
S = "1" + "2" 'Creates an integer result with a value of 3
S = "1" & "2" ' Creates a string result with a value of "12"

In other words, the "&" forced string concatenation, while "+" permitted
(but did not require) interpretation of strings as possible numeric values.

Is this still an issue in VB.Net? If so, then "&=" still has a valuable
function and should not be deprecated.

Richard Kucia
 
The '+' is the string concatenator in .net, so using it is OK.

I do have a further question though - what is the best way to empty a
string, or is it simply easier to use '=' to erase the previous contents
and replace with another?


Many thanks

Paul
 
Answered my own question: there is a significant distinction between "&" and
"+". From the MSDN topic "+ Operator":

"When you use the + operator, you may not be able to determine whether
addition or string concatenation will occur. Use the & operator for
concatenation to eliminate ambiguity and provide self-documenting code."

Richard Kucia
 
Paul said:
The '+' is the string concatenator in .net, so using it is OK.

I do have a further question though - what is the best way to empty a
string, or is it simply easier to use '=' to erase the previous contents
and replace with another?


Strings in CF are read-only objects. Assigning an empty string to a string
variable does not remove the previous string from memory. It will stay there
until garbage-collected. William has pretty much covered it earlier in this
thread

 
Back
Top