+ and & operators

  • Thread starter Thread starter Domac
  • Start date Start date
Domac said:
What is the difference between "+" and "&" operator when joining strings?

Did you already read the documentation on these operators?
 
*From:* "Domac" <[email protected]>
*Date:* Thu, 29 Jun 2006 13:51:07 +0200

What is the difference between "+" and "&" operator when joining
strings?

Not much difference when joining two real strings.
"aaa" + "bbb" would give the same result as "aaa" & "bbb" i.e. "aaabbb"

However, if one expression is a number and one a string then things are
less clear...

"5" + 10 will give the number 15. The "5" is converted to a number "on the
fly".

Whereas "5" & 10 will give the string "510". The 10 is converted to a
string "on the fly".

+ should really only be used to add numbers, and & should be used to
concatenate strings.
 
Domac said:
What is the difference between "+" and "&" operator when joining strings?

With "Option Strict On" - none at all.

With "Option Strict Off" and joining only Strings - none at all.

With "Option Strict Off" and joining Strings and /other/ data types -
the effects can be "interesting" ...

HTH,
Phill W.
 
Phill said:
With "Option Strict On" - none at all.

With "Option Strict Off" and joining only Strings - none at all.

With "Option Strict Off" and joining Strings and /other/ data types -
the effects can be "interesting" ...

HTH,
Phill W.

Except that you can do += but not &=.

B.
 
I use &= in assignment statements all the time. For example:

strHTML &= MonthName(CInt(sMonth), False) & " - " & sYear &
"</h3></center><br>"

Hope this helps...

Dick
 
Dick said:
I use &= in assignment statements all the time. For example:

strHTML &= MonthName(CInt(sMonth), False) & " - " & sYear &
"</h3></center><br>"

Hope this helps...

Dick

OK, i see, my mistake. Thanks for the correction.

I quickly tested it out by opening a module and typing:

Sub a()
Dim a As String
a+=a
a&=a
End Sub

The editor separated a+=a into a += a, but it separated a&=a to a& = a
and underlined a& as an error. Which i assumed (incorrectly) that &=
was not supported.

So, perhaps a difference is whether the editor put in that space before
the operator. With = is does, with & it does not. Well, nothing too
important. :)

Thanx for the correction.

B.
 
Excellent Answer.

Short, sweet, and most important a simple example.

I didnt know why either, till i seen this post.

Miro
- Learning VB Net
 
Brian said:
OK, i see, my mistake. Thanks for the correction.

I quickly tested it out by opening a module and typing:

Sub a()
Dim a As String
a+=a
a&=a
End Sub

The editor separated a+=a into a += a, but it separated a&=a to a& = a
and underlined a& as an error. Which i assumed (incorrectly) that &=
was not supported.

So, perhaps a difference is whether the editor put in that space before
the operator. With = is does, with & it does not. Well, nothing too
important. :)

Thanx for the correction.

B.

That is Basic Baggage. The & character once had a meaning at the end of
a variable name. The interpreter still recognises this, but it doesn't
work any more.
 
i dont know about myvar& not working but myvar% still does,
it still documented as working too

guy
 
Göran
| That is Basic Baggage. The & character once had a meaning at the end of
| a variable name. The interpreter still recognises this, but it doesn't
| work any more.
As Guy suggests whatever& still works in both .NET 1.x & .NET 2.0.

For example:

Dim whatever&

is short hand for:

Dim whatever As Long

For details see:

http://msdn2.microsoft.com/en-us/library/s9cz43ek.aspx

However I don't recommend using it, as only a very small subset of types are
supported...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Brian Tkatch wrote:
| > Dick Sutton wrote:
| >> I use &= in assignment statements all the time. For example:
| >>
| >> strHTML &= MonthName(CInt(sMonth), False) & " - " & sYear &
| >> "</h3></center><br>"
| >>
| >> Hope this helps...
| >>
| >> Dick
| >>
| >> | >>> Phill W. wrote:
| >>>> Domac wrote:
| >>>>> What is the difference between "+" and "&" operator when joining
| >>>>> strings?
| >>>> With "Option Strict On" - none at all.
| >>>>
| >>>> With "Option Strict Off" and joining only Strings - none at all.
| >>>>
| >>>> With "Option Strict Off" and joining Strings and /other/ data types -
| >>>> the effects can be "interesting" ...
| >>>>
| >>>> HTH,
| >>>> Phill W.
| >>> Except that you can do += but not &=.
| >>>
| >>> B.
| >>>
| >
| > OK, i see, my mistake. Thanks for the correction.
| >
| > I quickly tested it out by opening a module and typing:
| >
| > Sub a()
| > Dim a As String
| > a+=a
| > a&=a
| > End Sub
| >
| > The editor separated a+=a into a += a, but it separated a&=a to a& = a
| > and underlined a& as an error. Which i assumed (incorrectly) that &=
| > was not supported.
| >
| > So, perhaps a difference is whether the editor put in that space before
| > the operator. With = is does, with & it does not. Well, nothing too
| > important. :)
| >
| > Thanx for the correction.
| >
| > B.
| >
|
| That is Basic Baggage. The & character once had a meaning at the end of
| a variable name. The interpreter still recognises this, but it doesn't
| work any more.
 
Jay said:
Göran
| That is Basic Baggage. The & character once had a meaning at the end of
| a variable name. The interpreter still recognises this, but it doesn't
| work any more.
As Guy suggests whatever& still works in both .NET 1.x & .NET 2.0.

For example:

Dim whatever&

is short hand for:

Dim whatever As Long

For details see:

http://msdn2.microsoft.com/en-us/library/s9cz43ek.aspx

However I don't recommend using it, as only a very small subset of types are
supported...

Oh, so it still works, even?

Why didn't they get rid of that? They had the chance when they created
VB.NET, now it's gonna haunt the VB language for ever...
 
Back
Top