chuck
chuck

Reputation: 55

Simple VB using Ampersand

Quickie here, I'm sure...

I am going through some code written by a former employee and keep seeing the ampersand followed by an equals sign i.e. &=

I have not used this at all and was wondering why that would be used instead of just the ampersand.

Upvotes: 2

Views: 737

Answers (5)

psyklopz
psyklopz

Reputation: 2391

Remember that x = x + 1 and x =+ 1 are the same.

Also stringA = stringA & stringB and stringA &= stringB are the same.

Upvotes: 0

jfrankcarr
jfrankcarr

Reputation: 481

It just a different way to use the concatenation operator, just like += for numeric values. Either way is fine although for concatenating a lot of strings it's better to use StringBuilder.

Upvotes: 3

Oded
Oded

Reputation: 499072

& is the concatenation operator.

When using &= in an expression like:

string1 &= string2

This is the same as:

string1 = string1 & string2

The use of operator= is very common in languages like C, C#, C++, Java and more - they have constructs like -=, += and more, which simply mean that the variable in the left operand is to be used in the binary operation and the result assigned back to the variable.

MSDN puts it like this - &= Operator (Visual Basic):

Concatenates a String expression to a String variable or property and assigns the result to the variable or property.

Upvotes: 9

MichaelS
MichaelS

Reputation: 7103

ampersand in VB.NET is used to concatinate strings. You can use instead the + sign, but it is a better practice to use &.

Upvotes: 1

Rob Allen
Rob Allen

Reputation: 17729

From MSDN

Concatenates a String expression to a String variable or property and assigns the result to the variable or property.

Upvotes: 0

Related Questions