string concatenation

  • Thread starter Thread starter Guest
  • Start date Start date
Daddy,
I guess you know more than Microsoft.
Absolutly not, however it is made by persons who make as well as me
sometimes mistakes in there documentation and when I see things which are
strange for me, than I get the feeling, "here is something wrong".

But I found the answer from Larry better than my message so let's keep it
with that.

:-)

Cor
 
Cor Ligthert said:
what is the correct form of string concatenation in
VB.NET, + or &?
[...] The most correct is using stringbuilder


This is not true in all cases. Sometimes the concatenation done with '&'
can be done at compile time, so there will not be any performance loss at
runtime, for example:

\\\
Dim s As String = _
"He said " & _
ControlChars.Quote & "Hello World!" & ControlChars.Quote
///

For small strings and a large number of concatenations, using strings will
make fewer overhead than using a 'StringBuilder'.
 
Bonj said:
what is the correct form of string concatenation in VB.NET,
+ or &?

I prefer '&' over '+'. Notice that the two operators are not equal. In the
sample below, with 'Option Strict On', the 2nd line will not compile. '&'
will automatically convert non-string types to string in order to perform
the concatenation, '+' with 'Option Strict' set to 'On' will not do that:

\\\
Dim s1 As String = "Item " & 1 & ": Foo"
Dim s2 As String = "Item " + 1 + ": Foo"
Dim s3 As String = "Item " + CStr(1) + ": Foo"
///

So, using '+' + 'Option Strict On' will lead to more explicit code than
using '&'. Nevertheless, I think '&' is visually more explicit than '+' for
the reader of the code.
 
Bonj,
I know, but only in BAD VB6.

IMHO Then there's your answer...
Coming from C# I was unaware whether & might be used for binary bitmask
and,
but it seems the word "And" is what's used?
& : string concatenation
And : bitwise and
AndAlso : logical short circuit and
Or : bitwise or
OrElse : logical short circuit or

FWIW: Paul Vick's "The Visual Basic .NET Programming Language" from Addison
Wesley
is a good (right size, right content) desk reference to the VB.NET language
itself. Paul's book covers just the language, not the framework.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top