User59 User59 - 5 months ago 30
Vb.net Question

VB.Net concatenation syntax

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.

Answer


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!"