gpt4 book ai didi

vb.net - 理解赋值/比较 vb.net

转载 作者:行者123 更新时间:2023-12-02 07:07:31 26 4
gpt4 key购买 nike

这是我第一次使用 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)

欢迎任何形式的帮助,提前致谢!

最佳答案

等号 (=) 在 VB.NET 中用于两个完全不同的运算符。它用作赋值运算符以及相等测试运算符。角色计算的运算符取决于上下文。因此,例如,在本例中:

Dim x As Integer = 1
Dim y As Integer = 2
Dim z As Integer = x = y

您可能会认为,就像在其他语言(例如 C#)中一样,执行该代码后,xyz 会全部等于2。但是,VB 将第二个等号视为相等测试运算符。因此,实际上,它是这样做的:

If x = y Then
z = True
Else
z = False
End If

不过,您会注意到,我们随后尝试将 bool 值分配给整型变量。如果您有Option Strict On(正如您应该的那样),它不会允许您这样做。如果这确实是您想要做的,它会强制您将其转换为整数,这使得它更加明显:

z = CInt(x = y)

但是,它仍然令人困惑,因此通常在 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

关于vb.net - 理解赋值/比较 vb.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14445733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com