VB.NET 赋值中的双重赋值有何作用?
我(VB.NET 新手)正在对一个函数进行一些代码维护,该函数有时会抛出异常“将字符串“False”(或“True”)转换为 Integer 类型时出错”。我发现与此 someVal 等效的
是一个字符串,someFun1 返回一个整数,而 someFun2 将一个整数作为参数
...
someVal = someVal = someFun1()
...
someFun2(someVal)
...
我认为可能发生的情况是它试图将 someFun1 的返回值分配给 someVal,然后执行布尔检查是否 someVal 已经改变 - 但我不认为这是需要做的。
我的问题是 - 这个双重赋值 (someVal = someVal = someFun1()) 是否完成了我在 VB.NET 中不知道的任何事情?
另一个注意事项:我意识到存在整数到字符串再返回整数的隐式转换,但这不会导致任何问题,因为这些值应该始终保存一个数值(可以从整数和字符串隐式来回转换) ,对吧?)不是对或错 - 据我所知
I (new to VB.NET) am doing some code maintenance on a function that sometimes throws an exception "error converting string "False" (or "True") to type Integer." What I found was something equivalent to this
someVal is a string, someFun1 returns an Integer and someFun2 takes an Integer as a parameter
...
someVal = someVal = someFun1()
...
someFun2(someVal)
...
What I think might be happening is that it is trying to assign someFun1's return value into someVal, then perform a bool check as to whether someVal has changed - but I don't think that is what needs to be done.
My question is - does this double assignment (someVal = someVal = someFun1()) accomplish anything that I don't know about in VB.NET?
another note: I realize there are implicit casts of integer to string and back to integer, but that shouldn't be causing any problems, because the values should always hold a numerical value (which can be implicitly cast back and forth from Integer and String, right?) not True or False - as far as I can tell
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里令人困惑的是,等于运算符
=
与 VB.NET 中的赋值运算符=
相同。在 C# 中,上面的代码相当于首先执行布尔等于运算符
==
,然后将结果插入到someVal
中。这会失败,因为someVal
是int
,而不是bool
。换句话说,运行时将
someVal
与someFun1()
的返回值进行比较,返回True
或False
,并且未能将其转换为整数。这不是“双重赋值” - 它只是一种内联表示,更明显的是我们试图为
Integer
变量提供Boolean
。The confusion here is that the equals operator
=
is the same as the assignment operator=
in VB.NET. In C#, the code above would be equivalent towhere the boolean equals operator
==
is carried out first, and the result is inserted intosomeVal
. This fails, becausesomeVal
isint
, notbool
.In other words, the runtime is comparing
someVal
with the return value ofsomeFun1()
, returningTrue
orFalse
, and failing to cast that to an integer. This isn't a "double assignment" - it's just an inline representation ofwhere it is much more obvious that we're trying to give an
Integer
variable a value of typeBoolean
.