boolean VB expression returning false for integer 1
This is probably a really basic (no pun intended) question, but I can't seem to find an answer anywhere. Why does the result of func1 return False and func2 returns True? On every other test I have done, integer 1 is converted to boolean true and 0 to false. Works ok if I just set rtnValue to 1 or 0.
Public Functio开发者_如何学运维n func1() As Boolean
Dim rtnValue As Integer = 0
Return rtnValue = 1
End Function
Public Function func2() As Boolean
Dim rtnValue As Integer = 0
Return rtnValue = 0
End Function
I believe that what you are doing in "Return rtnValue = 1" is actually comparing whether rtnValue is equal to 1, not setting rtnValue to 1 and then returning rtnValue.
You are dangerously mixing up types. In VB, the integer 1 is integer 1, not boolean true. Same goes for integer 0 and boolean false.
In addition, your func1() is checking to see if rtnValue is equal to 1. If it is, then your function returns true. If not, it returns false. In your case, you set rtnValue to 0, and since 0 does not equal 1, it returns false.
Your func2() returns true because rtnValue is equal to 0, which is what you are testing.
You are retuning an expression check, it appears. Thus it will report as true
when Return rtnValue = 0
because rtnValue
is indeed 0
.
精彩评论