问题
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!
回答1:
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
回答2:
Let's look at this line of code:
dictionary = Me.demandas2.Item(label3 = label) = (dictionary.Item(label3) - 1)
The first =
is an assignment. So we assign the right part to the dictionary. Now for the right part:
Me.demandas2.Item(label3 = label) = (dictionary.Item(label3) - 1)
The =
between the two expressions is a comparison, so it returns a Boolean. So the supposed "dictionary" is assigned a boolean value. If we check the left part of that expression:
Me.demandas2.Item(label3 = label)
Once again, the =
sign here is doing a comparison, so if label3
is the same as label
, then the code would be equivalent to Me.semandas2.Item(True)
. This seems strange.
Overall, this code doesn't make much sense, and I'd be surprised if it compiled, considering it tries to assign a boolean to a dictionary. It certainly wouldn't compile with Option Strict On
.
回答3:
Thanks a lot, everyone. The snippet was result of decompile a dll. I was trying to help a partner.
.Net reflector decompiled based on VB.Net code, that was a mistake. Finally we see that first it should decompile using C# code, that gives a complete different meaning to the code:
if (movimiento.Contenedor.Demanda2)
{
Dictionary<Label, int> dictionary;
Label label3;
(dictionary = this.demandas2)[label3 = label] = dictionary[label3] - 1;
if (this.demandas2[label] == 0)
{
label.ForeColor = Color.Black;
}
(dictionary = this.demandas2)[label3 = label2] = dictionary[label3] + 1;
label2.ForeColor = Color.DarkOrange;
}
来源:https://stackoverflow.com/questions/14445733/understanding-assignment-comparison-vb-net