This is my first time on Stack Overflow and I am trying to understand what '=' means in the last line of this code:
Dim label As Label = Me.labels.Item(String.Concat(New Object() { movimiento.Sector1.ID, "-", movimiento.X1, "-", movimiento.Y1 }))
Dim dictionary As Dictionary(Of Label, Integer)
Dim label3 As Label
dictionary = Me.demandas2.Item(label3 = label) = (dictionary.Item(label3) - 1)
Any kind of help will be welcome, thanks in advance!
The equals sign (=
) is used for two entirely different operators in VB.NET. It is used as the assignment operator as well as for the equality test operator. The operator, to which the character evaluates, depends on the context. So, for instance, in this example:
Dim x As Integer = 1
Dim y As Integer = 2
Dim z As Integer = x = y
You might think, as in other languages, such as C#, that after executing that code, x
, y
, and z
would all equal 2
. However, VB treats the second equals sign as an equality test operator. Therefore, in actuality, it's doing this:
If x = y Then
z = True
Else
z = False
End If
You'll notice, though, that we are then trying to assign a boolean value to an integer variable. If you have Option Strict On
(as you should), it would not allow you to do that. If that's really what you wanted to do, it would force you to cast it to an integer, which makes it slightly more obvious:
z = CInt(x = y)
However, it's still confusing, so typically, this kind of thing is discouraged in VB.NET. So, I suspect that the code you posted wouldn't even compile if Option Strict
was turned on. But, this is what it's actually trying to do:
Dim temp1 As Boolean = (label3 = label) ' Evaluates to False
Dim temp2 As Boolean = (Me.demandas2.Item(temp1) = (dictionary.Item(label3) - 1)) ' Likely evaluates to False
dictionary = temp2 ' Couldn't possibly be a valid assignment
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With