Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am attempting to get the average cost of fuel purchased per trip.

My code snippets are as follows:

Private Sub calc()

        Dim 
            a, f1, f2, f3, f4, f5, f6 As Decimal
        Try
            f1 = CDec(IIf(tbTruckFuelPurchased.Text.Trim = "", 0D, tbTruckFuelPurchased.Text.Trim))
            f2 = CDec(IIf(tbReeferFuelPurchased.Text.Trim = "", 0D, tbReeferFuelPurchased.Text.Trim))
            f3 = CDec(IIf(tbDEFPurchased.Text.Trim = "", 0D, tbDEFPurchased.Text.Trim))
            f4 = CDec(IIf(tbGalsPurch.Text.Trim = "", 0D, tbGalsPurch.Text.Trim))
            f5 = CDec(IIf(tbREF_Gals.Text.Trim = "", 0D, tbREF_Gals.Text.Trim))
            f6 = CDec(IIf(tbFuelCostAvg.Text.Trim = "", 0D, tbFuelCostAvg.Text.Trim))
        Catch ex As Exception
            'If a calculation error occurs, show Error message box
            Dim frm As New MeMsgCalcError(ex, "Error in 'Calculation DIM Statement' Defining Area.")
            frm.Show()
        End Try


        'Calculate Average Cost of Fuel this Load
        Try
            a = f1 \ f4
            tbFuelCostAvg.Text = a.ToString("C2")
        Catch ex As Exception
            'If a calculation error occurs, show Error message box
            Dim frm As New MeMsgCalcError(ex, "Calculation Error in 'Average Cost of Fuel this Load.'")
            frm.Show()
        End Try


This errors out noting:
Error BC30512 Option Strict On disallows implicit conversions from 'Decimal' to 'Long'.

As noted above in the error message, Option Strict is On and needs to be so.

How might I take the total fuel purchased (tbTruckFuelPurchased (currency)) and divide total fuel gallons purchased (tbGalsPurch (whole number plus 3 decimals) to end up with an average cost per gallon? Obviously I am looking for a "money" returned value rounded to 2 decimals.

I am not understanding the conversion from Decimal to Long error message.

What I have tried:

See message for code and tries.
Posted
Updated 10-Aug-16 8:11am
v3
Comments
Richard Deeming 10-Aug-16 13:51pm    
Where have you defined the variable a, and what data type is it?
K3JAE 10-Aug-16 14:06pm    
Apologize: I cut this out of the code... var a is included in the DIM f1-f6 statement so essentially add a to the DIM statement.

I will correct the code snippet above.

The problem is with this line:
a = f1 \ f4

The \ operator performs an integer division, and returns an integer type appropriate for the types of the operands:
\ Operator (Visual Basic)[^]

You need to perform a standard division, using the / operator:
a = f1 / f4
 
Share this answer
 
Comments
K3JAE 10-Aug-16 14:20pm    
Mr Deeming has hit the problem - I had the Divide by "sign" backwards. DOH!!!
Richard Deeming 10-Aug-16 14:25pm    
Two options:

1) Change your variables from Decimal to Double (and change CDec to CDbl). You'll then get either Double.NaN, Double.PositiveInfinity, or Double.NegativeInfinity, depending on the numerator.

2) Use the If statement to bypass the calculation if the denominator is zero:
a = If(f4 = 0, 0, f1 / f4)
Maciej Los 10-Aug-16 14:42pm    
5ed!
Since all of your "fn" variables are declared as Decimal, and there is only one other assignment in the code snippet, it has to be this line:
VB
a = f1 \ f4
Which means that a has to be a Long and option strict won't let you just assign a Decimal value to a Long variable since it would have to throw away any data to the right of the decimal point. in order to do it - Long is an integer data type that can't hold fractional values.
So either check and change the definition of a to Decimal, or cast the Decimal to a Long value:
VB
a = CLng(f1) \ CLng(f4)


BTW: do yourself a favour and don't use variable names like f1, f2, or a - use longer names that are more descriptive of what the value holds. It makes your code a lot more readable and more reliable!

[edit]changign a won;t help - I forgot the the \ operator throws the error if Option Stricty is on and you supply floating point values: https://msdn.microsoft.com/en-us/library/0e16fywh.aspx?f=255&MSPPError=-2147217396[^] [/edit]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900