Question Maths mystery

gchq

Well-known member
Joined
Dec 14, 2007
Messages
168
Programming Experience
10+
This is a really odd one! This has worked without incident, now it would appear that two numbers that add up to a total do not!!

VB.NET:
        Dim vNet As Double = AmountNetTB.Text
        Dim vTax As Double = AmountSalesTaxTB.Text
        Dim vTotal As Double = AmountTotalTB.Text
        AppBox.Show(vNet.ToString & " " & vTax.ToString & " " & vTotal.ToString & " - " & vNet + vTax, MessageBoxButtons.OK, MessageBoxIcon.Information)
        If Not vTotal = (vNet + vTax) Then
            Return "The invoice total doesn't balance with the Sales Tax and the Net amount!"
        End If

I've checked and rechecked . vNet is returning 560.23 - vTax is returning 41.32 and vTotal is returning 601.55 - according to my maths that is correct, but it's not passing the if function

Any ideas?

Thanks


Update - tried it this way as well

VB.NET:
        Dim vNet As Double = AmountNetTB.Text
        Dim vTax As Double = AmountSalesTaxTB.Text
        Dim vTotal As Double = AmountTotalTB.Text
        Dim vRunTotal As Double = vNet + vTax

        If vTotal <> vRunTotal Then
            AppBox.Show(vTotal & " - " & vRunTotal, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Return "The invoice total doesn't balance with the Sales Tax and the Net amount!"
        End If

The messagebox is returning 601.55 for both vTotal and vRunTotal and yet it's still getting caught in the If statement! This one is REALLY driving me up the proverbial wall......
 
Last edited:
1. Use Decimal instead of Double.

2. Use Option Strict On to avoid bad coding such as :-

VB.NET:
Dim vNet As Decimal = AmountNetTB.Text
 
Why is that bad coding?

Seems the textbox calculation function was causing this - not sure how, but math.round sorted the problem out!

Thanks for your reply
 
What if I put "AB" in that TextBox? Your program would crash in an instant.

Not so - all textboxes run through a vaildate string function first like

If not IsDate(TextBox1.Text) then
Return "The date is not valid"
End If

If the data is not valid the textback back colour is changed to yellow, the textbox gets focus and the code stops running... At the end of the function the final return is "Validated" - so the sub that either inserts or updates data first runs that function...

If not ValidateString = "Validated" Then
Appbox.Show(ValidateString, .....)
Exit Sub
End If
 
Why is that bad coding?

Because no matter how much validation you perform, AmountNetTB.Text will always be exactly that - text. Not decimal, not double - plain old text. Therefore :-

VB.NET:
Dim vNet As Decimal = AmountNetTB.Text

will always be incorrect, because vNet can never be set to AmountNetTB.Text - you have to convert the text to a decimal. Either use a simple

VB.NET:
Dim vNet As Decimal = Convert.ToDecimal (AmountNetTB.Text)

or investigate TryParse.
 
But if it's already been validated (IsDate, IsNumeric... - and a host of custom methods like IsCurrency) and passed that, then it must be safe to assume that the data will convert when it hits that line of code. Why, in effect, check it again?

Whilst I never stop learning, I can state that the method I use has never thrown an error to date!
 
You are confused about the purpose of the IsNumeric function. It checks to be sure that the text in the textbox can safely converted into a number, i.e. there are only valid digits in the box. It DOESN'T convert the string in the box to a number. You are adding a valid string representation of a number to the valid string representation of another number but that certainly isn't the same as adding the first number to second number. You have to do the conversion the previous poster described.

Just in case you don't believe me, here is a link:

IsNumeric Function (Visual Basic)
 
Last edited:
You are confused about the purpose of the IsNumeric function

Au contraire - I am not confused at all. The text in the TextBoxes is validated first, then converted to the the appropriate type(Date, Double...) and THEN calculations are carried out on the Type and/or saved to the backend DB.
 
VB.NET:
Dim vNet As Double = AmountNetTB.Text
VB.NET:
Dim vNet As Double = CDbl(AmountNetTB.Text)
The only difference between these is who does the conversion, you explicitly, or compiler implicitly. If you turn on Option Strict you are forced to handle the type conversions/casts explicitly, this is something I recommend and will help you write better type safe code (implicit often means unaware). In most cases Intellisense will identify possible type conversion errors/problems and suggest the remedy with a simple click in error correction dialog.

The CDbl conversion from user input is something you can only do if you have already validated that the string can be converted to Double, since if it can't an exception will be thrown. If there is no input validation one option is to Try-Catch the expected possibility for conversion failure, another is to use Double.TryParse which in a single operation will try to convert and tell you the outcome without throwing exception.

About the "mystery" this is also something you should read: Troubleshooting Data Types (Visual Basic)
I'd use the Decimal data type.
 
Hi John

Thank you for your concise explanation. Since I started this in 1985 most of it has been self taught - and asp (classic), when it came along, didn't do a lot to promote good coding practices.

When I start on the next project...
 
Just turn the Option Strict ON. I am not sure why they say that but this what every programmer will advise you. I am also confused why they have given relaxation over that.
VB is and have always been a 'relaxed' language that is suitable for beginners to learn and advance rapidly. There is nothing wrong with implicit conversions/casts, especially if you know what you're doing and don't make programming errors. Though later VS versions is much more advanced and turning on Option Strict is no longer such a liability when you don't know what to do, because code editor now can now suggest error correction. Types are also much easier to handle with new features such as type inference, and intellisense is really helpful with autocomplete and valid member lists when writing code. Option Strict enforces stricter type handling, but then it also offer better code writing help from the editor.
 
Back
Top