理解赋值/比较 vb.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14445733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding assignment/comparison vb.net
提问by Cuadrunga
This is my first time on Stack Overflow and I am trying to understand what '=' means in the last line of this code:
这是我第一次使用 Stack Overflow,我试图理解这段代码最后一行中的“=”是什么意思:
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!
欢迎任何形式的帮助,提前致谢!
采纳答案by Cuadrunga
Thanks a lot, everyone. The snippet was result of decompile a dll. I was trying to help a partner.
非常感谢大家。该片段是反编译 dll 的结果。我试图帮助一个合作伙伴。
.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:
基于VB.Net代码反编译.Net反射器,这是一个错误。最后我们看到它首先应该使用 C# 代码进行反编译,这给代码带来了完全不同的含义:
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;
}
回答by Steven Doggart
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:
等号 ( =) 用于 VB.NET 中两个完全不同的运算符。它用作赋值运算符以及相等测试运算符。字符计算的运算符取决于上下文。所以,例如,在这个例子中:
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 zwould all equal 2. However, VB treats the second equals sign as an equality test operator. Therefore, in actuality, it's doing this:
您可能会认为,就像在其他语言(例如 C#)中一样,在执行该代码后x,y, 和z将都等于2. 但是,VB 将第二个等号视为相等测试运算符。因此,实际上,它是这样做的:
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:
但是,您会注意到,我们正在尝试将布尔值分配给整数变量。如果你有Option Strict On(正如你应该的那样),它不会允许你这样做。如果这真的是您想要做的,它会强制您将其强制转换为整数,这使其更加明显:
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 Strictwas turned on. But, this is what it's actually trying to do:
但是,它仍然令人困惑,因此通常情况下,VB.NET 不鼓励这种事情。因此,我怀疑您发布的代码即使Option Strict打开也无法编译。但是,这就是它实际尝试做的事情:
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
回答by Meta-Knight
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 label3is the same as label, then the code would be equivalent to Me.semandas2.Item(True). This seems strange.
再一次,=这里的符号是在做比较,所以如果label3与 相同label,那么代码就等价于Me.semandas2.Item(True)。这看起来很奇怪。
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.
总的来说,这段代码没有多大意义,如果它被编译,我会感到惊讶,因为它试图为字典分配一个布尔值。它肯定不会与Option Strict On.

