开发者

What Does a double assignment in a VB.NET assignment accomplish?

开发者 https://www.devze.com 2023-01-07 06:00 出处:网络
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 equival

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 = som开发者_JAVA百科eVal = 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


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 to

someVal = someVal == someFun1();

where the boolean equals operator == is carried out first, and the result is inserted into someVal. This fails, because someVal is int, not bool.

In other words, the runtime is comparing someVal with the return value of someFun1(), returning True or False, and failing to cast that to an integer. This isn't a "double assignment" - it's just an inline representation of

If someVal = someFun1() Then
    someVal = True
Else
    someVal = False
End If

where it is much more obvious that we're trying to give an Integer variable a value of type Boolean.

0

精彩评论

暂无评论...
验证码 换一张
取 消