Re: Simple Cast not working
Thats because either tbCalcFF.Text or tbGoalFF.Text is equal to "", you cant turn that into a valid single value.
What you should do is use Single.TryParse, its handy whenever you are unsure if the string can be converted or not.
VB Code:
Dim value1 As Single
Dim value2 As Single
If Single.TryParse(tbCalcFF.Text, value1) AndAlso Single.TryParse(tbGoalFF.Text, value2) THen
Dim varFF As Single = value1 - value2
If varFF < 0 Then
tbCalcFF.BackColor = Color.Gold
Else
tbCalcFF.BackColor = Color.LightGoldenrodYellow
End If
Else
MessageBox.Show("Enter valid single values!")
End If
Re: Simple Cast not working
Another thing you might try, along the same lines, would be to split that one If statement into two, because if the return from either one is false, you could simply put a 0 into that value on the assumption that an empty string is basically 0 (or some other default value if something else makes sense).
Come to think of it, that's kind of obvious, but sometimes we try to make nice neat, tight, code, and sacrifice useability.
Re: Simple Cast not working
Quote:
Originally Posted by Shaggy Hiker
Another thing you might try, along the same lines, would be to split that one If statement into two, because if the return from either one is false, you could simply put a 0 into that value on the assumption that an empty string is basically 0 (or some other default value if something else makes sense).
Come to think of it, that's kind of obvious, but sometimes we try to make nice neat, tight, code, and sacrifice useability.
Yeah good thinking. Then I suppose this would work aswell.
VB Code:
Dim value1 As Single
Dim value2 As Single
Single.TryParse(tbCalcFF.Text, value1)
Single.TryParse(tbGoalFF.Text, value2)
Dim varFF As Single = value1 - value2
If varFF < 0 Then
tbCalcFF.BackColor = Color.Gold
Else
tbCalcFF.BackColor = Color.LightGoldenrodYellow
End If
Because value1 and value2 will keep their default values of 0 if TryParse fails. However, I suppose that using an IF statement to check wether the textboxes are empty or not as you mentioned would be more efficient.