tags:

views:

32

answers:

1

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

+2  A: 

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.

Tomas Lycken
I just realized I got the types of your variables wrong, but the same principles apply for strings etc.
Tomas Lycken