I am looking to find out why I am getting an error message when I run this application. The message box works correctly, but that's about it. The error message I am getting is:
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "C2" to type 'Integer' is not valid.
If anyone can point me into the right direction I would greatly appreciate it. This is for an assignment, so please just provide guidance so that I may correct the problem. Below is the sections of code that I am having issues with. Two independent subs and a click_event. The assignment is specific in having 2 separate subs that 1) calculate and 2)display. I have a feeling it may be the way I am trying to pass the variables/parameters but I am not sure.
Private Sub txtPrevReadng_TextChanged(sender As Object, e As EventArgs) Handles txtPrevReadng.TextChanged
'clears labels when text box altered
lblTotCharge.Text = String.Empty
lblGalUsed.Text = String.Empty
End Sub
'Independent Sub to make calculations********
Private Sub GetGalPrice(ByVal decPricePG, ByVal decCurrentRead, ByVal decPrevRead, ByRef decNewGal, ByRef decSubTot)
decNewGal = decCurrentRead - decPrevRead
decSubTot = decNewGal * decPricePG
End Sub
'Independent sub to display results********
Private Sub Display(ByVal decMinMC, ByVal decSubTot, ByVal decNewGal)
If decSubTot > decMinMC Then
lblTotCharge.Text = decSubTot.ToString("C2")
lblGalUsed.Text = decNewGal.ToString("N2")
Else
lblTotCharge.Text = decMinMC.ToString("C2")
lblGalUsed.Text = decNewGal.ToString("N2")
End If
End Sub
'Calc Click Event -- call subs to calculate and display bill*********
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
Const decPricePG As Decimal = 0.00515
Const decMinMC As Decimal = 19.69
Dim decCurrentRead As Decimal
Dim decPrevRead As Decimal
Dim decNewGal As Decimal
Dim decSubTot As Decimal
Decimal.TryParse(txtCrrntReadng.Text, decCurrentRead)
Decimal.TryParse(txtPrevReadng.Text, decPrevRead)
If decCurrentRead < decPrevRead Then
MessageBox.Show("Current reading must be greater or equal to previous reading.", "Invalid Current Reading Entered", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
Call GetGalPrice(decPricePG, decCurrentRead, decPrevRead, decNewGal, decSubTot)
End If
Call Display(decMinMC, decSubTot, decNewGal)
End Sub
End Class
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "C2" to type 'Integer' is not valid.
If anyone can point me into the right direction I would greatly appreciate it. This is for an assignment, so please just provide guidance so that I may correct the problem. Below is the sections of code that I am having issues with. Two independent subs and a click_event. The assignment is specific in having 2 separate subs that 1) calculate and 2)display. I have a feeling it may be the way I am trying to pass the variables/parameters but I am not sure.
Private Sub txtPrevReadng_TextChanged(sender As Object, e As EventArgs) Handles txtPrevReadng.TextChanged
'clears labels when text box altered
lblTotCharge.Text = String.Empty
lblGalUsed.Text = String.Empty
End Sub
'Independent Sub to make calculations********
Private Sub GetGalPrice(ByVal decPricePG, ByVal decCurrentRead, ByVal decPrevRead, ByRef decNewGal, ByRef decSubTot)
decNewGal = decCurrentRead - decPrevRead
decSubTot = decNewGal * decPricePG
End Sub
'Independent sub to display results********
Private Sub Display(ByVal decMinMC, ByVal decSubTot, ByVal decNewGal)
If decSubTot > decMinMC Then
lblTotCharge.Text = decSubTot.ToString("C2")
lblGalUsed.Text = decNewGal.ToString("N2")
Else
lblTotCharge.Text = decMinMC.ToString("C2")
lblGalUsed.Text = decNewGal.ToString("N2")
End If
End Sub
'Calc Click Event -- call subs to calculate and display bill*********
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
Const decPricePG As Decimal = 0.00515
Const decMinMC As Decimal = 19.69
Dim decCurrentRead As Decimal
Dim decPrevRead As Decimal
Dim decNewGal As Decimal
Dim decSubTot As Decimal
Decimal.TryParse(txtCrrntReadng.Text, decCurrentRead)
Decimal.TryParse(txtPrevReadng.Text, decPrevRead)
If decCurrentRead < decPrevRead Then
MessageBox.Show("Current reading must be greater or equal to previous reading.", "Invalid Current Reading Entered", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
Call GetGalPrice(decPricePG, decCurrentRead, decPrevRead, decNewGal, decSubTot)
End If
Call Display(decMinMC, decSubTot, decNewGal)
End Sub
End Class