string concatenation

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

what is the correct form of string concatenation in VB.NET, + or &?
Both seem to work, but which is correct?
I know it's + in C# and & in VB6, but which in VB.NET?
 
Bonj,
what is the correct form of string concatenation in VB.NET, + or &?
Both seem to work, but which is correct?
I know it's + in C# and & in VB6, but which in VB.NET?

The most correct is using stringbuilder

When you do not want that for whatever reason than the second correct is the
&

When you want to see a little sample of stringbuilder, reply that than?

I hope this helps?

Cor
 
The most correct is using stringbuilder

Is it? Why do you say 'most' correct, surely either it's correct or it's not?
You saying that leads me to believe that you think that because
StringBuilder is fast at appending a lot of small strings to one big, I
notice it does have an append method.

When I said "what is correct", what I meant was, I thought that maybe it was
something like, MS had wanted everybody to use the +, but left & in for
backward compatibility reasons with VB6, or maybe only introduced + to be
similar to C# but really & was what they wanted you to use.
When you want to see a little sample of stringbuilder, reply that than?

Yes, go on then - if you've got one...
cheers
 
Bonj,
When I said "what is correct", what I meant was, I thought that maybe it
was
something like, MS had wanted everybody to use the +, but left & in for
backward compatibility reasons with VB6, or maybe only introduced + to be
similar to C# but really & was what they wanted you to use.

No it is not backward compatible, in case of + or & is the last the most
correct for string concatination.

Try this with option Strict off
MessageBox.Show(1 & 2)
MessageBox.Show(1 + 2)

Both compiles and both shows an answer however those are different.

When the + is seen for concatination in this newsgroup, it mostly get a
message with the question why the + is used and that it has to be the &.

I had to answer that question about the stringbuilder as well because it is
even with small concatinations often much faster, but when it is one time in
a program, just use the &.

I hope that this answers your question?

Cor
 
You missed the boat a bit here Cor.

The OP was asking about concatenating strings, not conactenating numbers.

Your 2 examples will produce the correct results if the operands are
actually strings:

MessageBox.Show("1" & "2")
MessageBox.Show("1" + "2")

You have to remember that the StringBuilder class is a hybrid of the String
class with a lot more oomph for where it is needed or desired. It's use is
not mandatory.

As for the '+' operator for string concatenation, this is included for
backward compatability with the earliest dialects of BASIC where the only
string concatenation operator was '+'. The '&' string contenation operator
was introduced later so that confusion could be avoided, especially when
concatenating numbers that were coerced into strings.

The upshot is that the '&' string concatenation operator is the preferred
one.
 
Stephany,
You missed the boat a bit here Cor.
The OP was asking about concatenating strings, not conactenating numbers.

Your 2 examples will produce the correct results if the operands are
actually strings:

MessageBox.Show("1" & "2")
MessageBox.Show("1" + "2")
Those are correct as well with option string on.

MessagBox.Show(1 & 2), when option strict is of, is this a string
concatination done by late binding.
The upshot is that the '&' string concatenation operator is the preferred
one.

My text "No it is not backward compatible, in case of + or & is the last the
most
correct for string concatination".

What is beside the words the difference?

And therefore what boat did I miss, you make me curious?

Cor
 
Bonj said:
what is the correct form of string concatenation in VB.NET, + or &?
Both seem to work, but which is correct?
I know it's + in C# and & in VB6, but which in VB.NET?

A quick peek under the covers shows there is no difference in
which you use when the operands are both strings, but as was
the case in VB6, you should use & to remove any ambiguity as
to what operations will be performed.

All that is available in the help file. Highlight a + sign and hit F1.

LFS
 
Cor,

The issue is that 1 and 2 are numbers, not strings. Therefore, when you
call MessageBox(1 + 2), they are treated as numbers and return 3 (as an
integer). However, when you call MessageBox(1 & 2), the operands are
coerced into string representation and are properly concatenated (as
strings) to "12".

Your example shows precisely why the ampersand operator ("&") was
introduced: because the plus sign ("+") is ambiguous in its meaning outside
of a strictly mathematical interpretation. That's why Stephany said that
the ampersand is preferred.

Hope that helps?
Derrick
 
Bonj,
+ is also used to concatenate strings in VB6!

So which you use is really up to you, and what you are doing on that line.

I use & as it is the concatenation operator, while + is the addition
operator.

If you have Option Strict Off, the + may convert its arguments to or from
String first before performing the Addition.

StringBuilder has its place as does + and &. If I have a loop I will use a
StringBuilder, where as if I have a single line I will probably use &.

Of course if profiling proved that one was performing badly in a specific
routine, I would try the other to see if that improved performance.

I would not combine StringBuilder and & in a single line, such as
concatenating two strings to pass to StringBuilder.Append, as that is rarely
correct...

Hope this helps
Jay
 
Daddy,

Can you tell me why they write this in the same paragraph

In ASP.NET applications, consider emitting HTML output by using multiple
Response.Write calls instead of using a StringBuilder.

That means in my opinion that Visual Studio Net becomes the same as a
notepad.

Cor
 
Derrick.

Why are you repeating my message?

Try to help somebody yourself and do not copy my messages as if you did that
and that I wrote something else.

Cor

"Derrick [MCSD]"
....
 
Cor,

First, my intention was not to "repeat your message", but to help you
understand why Stephany wrote what she did. I think the misunderstanding
was in your example, but maybe we misread it. I think we've all been saying
the same thing, just differently.

Second, it looks like this issue (a trivial explanation) has become
emotionally entangling. It was not my intention to offend, and I apologize
if I did.

Third, I believe Bonj has MORE than had his question answered, so I will
disengage from this thread.

Regards,
Derrick



Cor Ligthert said:
Derrick.

Why are you repeating my message?

Try to help somebody yourself and do not copy my messages as if you did that
and that I wrote something else.

Cor

"Derrick [MCSD]"
...
Cor,

The issue is that 1 and 2 are numbers, not strings. Therefore, when you
call MessageBox(1 + 2), they are treated as numbers and return 3 (as an
integer). However, when you call MessageBox(1 & 2), the operands are
coerced into string representation and are properly concatenated (as
strings) to "12".

Your example shows precisely why the ampersand operator ("&") was
introduced: because the plus sign ("+") is ambiguous in its meaning
outside
of a strictly mathematical interpretation. That's why Stephany said that
the ampersand is preferred.

Hope that helps?
Derrick



last
the
 
How mandatory is the ' in "It's" ?


Stephany Young said:
You missed the boat a bit here Cor.

The OP was asking about concatenating strings, not conactenating numbers.

Your 2 examples will produce the correct results if the operands are
actually strings:

MessageBox.Show("1" & "2")
MessageBox.Show("1" + "2")

You have to remember that the StringBuilder class is a hybrid of the String
class with a lot more oomph for where it is needed or desired. It's use is
not mandatory.

As for the '+' operator for string concatenation, this is included for
backward compatability with the earliest dialects of BASIC where the only
string concatenation operator was '+'. The '&' string contenation operator
was introduced later so that confusion could be avoided, especially when
concatenating numbers that were coerced into strings.

The upshot is that the '&' string concatenation operator is the preferred
one.
 
Bonj,
+ is also used to concatenate strings in VB6!

I know, but only in BAD VB6.

I use & as it is the concatenation operator, while + is the addition
operator.

Coming from C# I was unaware whether & might be used for binary bitmask and,
but it seems the word "And" is what's used?
(If you ask why I go from C# to VB.NET, it's because the amount of times
I've typed "using SYstem ..." and done a load of backspaces and retyped it
with "using System... " I must be able to write one more complete project per
year.)
If you have Option Strict Off, the + may convert its arguments to or from
String first before performing the Addition.

I always have option strict on.
StringBuilder has its place as does + and &. If I have a loop I will use a
StringBuilder, where as if I have a single line I will probably use &.

Of course if profiling proved that one was performing badly in a specific
routine, I would try the other to see if that improved performance.

I would not combine StringBuilder and & in a single line, such as
concatenating two strings to pass to StringBuilder.Append, as that is rarely
correct...

Hope this helps

Yes, thanks very much
 
Dr Screwup said:

This is a VB.Net group. References to C# may be confusing in that
in C# the & is entirely different than VB's & operator. In C# the only
operator to concatenate strings is the + operator, not so in VB. If you
note, most of the code on that page was C#. Had they said use &, it
would have been totally wrong for the C# coders.

As I suggested to Borj, highlight a + in a VB module and hit F1 for
VB's own documentation on that operator....

You've got to keep your eye on the ball!

LFS
 
You're right I was asking about concatenating strings, but I think Cor was
right to point out that
1 + 2
1 & 2
can have different results and thus is a perfectly good reason why & should
be used rather than + for string concatenation (or concatenation of things
that should be interpreted as strings).
I was asking in the more broader sense of what is the *general* accepted
best method of concatenating strings *any time*, not just the particular
string variables I happen to have in this specific program, which I know for
a fact aren't going to be interpreted as numbers anyway as they're filenames.

But thanks for your help anyway
 

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