Reputation: 1180
I tried to google the answer for this but could not find it. I am working on VB.Net. I would like to know what does the operator += mean in VB.Net ?
Upvotes: 4
Views: 16565
Reputation: 1
Just makes code more efficient -
Dim x as integer = 3
x += 1
'x = 4
is the same as
x = x + 1
'x = 4
It can also be used with a (-):
x -= 1
' x = 2
Is the same as
x = x - 1
'x = 2
Upvotes: 0
Reputation: 22814
It is plus equals. What it does is take the same variable, adds it with the right hand number (using the + operator), and then assigns it back to the variable. For example,
Dim a As Integer
Dim x As Integer
x = 1
a = 1
x += 2
a = a + 2
if x = a then
MsgBox("This will print!")
endif
Upvotes: 2
Reputation: 4498
those 2 lines compiled produce the same IL code:
x += 1
and
x = x + 1
Upvotes: 1
Reputation: 3622
a += b
is equivalent to
a = a + b
In other words, it adds to the current value.
Upvotes: 5
Reputation: 43743
It means that you want to add the value to the existing value of the variable. So, for instance:
Dim x As Integer = 1
x += 2 ' x now equals 3
In other words, it would be the same as doing this:
Dim x As Integer = 1
x = x + 2 ' x now equals 3
For future reference, you can see the complete list of VB.NET operators on the MSDN.
Upvotes: 12