1

Around the we i have seen a lot of code written with these opcodes; += and &= I know that they are related to concatenation. So can someone explain to me what is the difference between += and &= compared to + and &.

Thank you in advance.

User59
  • 487
  • 4
  • 19
  • 5
    `+=` is addition. SImpler than `var = var + 1`; `&` is used for strings, `&=` would be for a small loop to concat some values to a longer string – Ňɏssa Pøngjǣrdenlarp Feb 29 '16 at 22:48
  • 2
    In addition to Plutonix's comment, `+` _CAN_ be used for String concatenation but is **not recommended** as it may cause exceptions in certain cases. `&` should always be used for String concatenation. – Visual Vincent Mar 01 '16 at 13:02

2 Answers2

1


The += operator is short form of X = X + Y The + operator is usually used for summing number rather than string combination(See Here). Example:

'Setting Values
Dim Var As Integer
Var = 101
'Adding 62 to this number (SHORT FORM)
Var += 62 'This will set Var to 163
'Reset value
Var = 101
'This is standard long form
Var = Var + 62 'This will again set Var to 163

The &= operator is short form of String1 = String1 & String2 The & operator is string combination. Example:

'Setting Values
Dim String1 As String
String1 = "coding is "
'combine "Great" to this string (SHORT FORM)
String1 += "Great!" 'This will set String1 to "coding is Great!"
'Reset Value
String1 = "coding is "
'This is standard long form
String1 = String1 & "Great!" 'This will again set String1 to "coding is Great!"
Community
  • 1
  • 1
Hirbod Behnam
  • 577
  • 2
  • 7
  • 26
0

There is actually no difference, it is just longer to write this

MyString = MyString & "some text"

than this

MyString &= "some text"

Since programmers are very lazy persons...

Same for the plus symbol for numbers. (I know it can also be used for strings but it is not recommanded...)

Martin Verjans
  • 4,675
  • 1
  • 21
  • 48