I had a student's code not working. He was getting this error: A first chance exception of type 'System.FormatException' occurred in mscorlib.dll. Goggleing the error was no help. My first reaction was to take my working code and compare it to his broken code. See the examples below. Can you see the difference? I did not. The bad habit I had was taking my working code and comparing it line by line after the lines by line. I have fixed many student errors this way. It did not work this time. I also let the student see the code also. I was unable to see the error but the student found the error later.
Working Code:
btnDisplayCost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayCost.Click
Dim strEnterNumberOfTickets As String
Dim intEnterNumberOfTickets As Integer
Dim decTotalCostOfTickets As Decimal
strEnterNumberOfTickets = Me.txtEnterNumberOfTickets.Text
intEnterNumberOfTickets = Convert.ToInt32(strEnterNumberOfTickets)
decTotalCostOfTickets = intEnterNumberOfTickets * cdecTotalCostOfTickets
Me.lblTotalCostOfTickets.Text = decTotalCostOfTickets.ToString("C")
Bad Code:
frmConcertTickets_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strEnterNumberOfTickets As String
Dim intEnterNumberOfTickets As Integer
Dim decTotalCostOfTickets As Decimal
strEnterNumberOfTickets = Me.txtEnterNumberOfTickets.Text
intEnterNumberOfTickets = Convert.ToInt32(strEnterNumberOfTickets)
decTotalCostOfTickets = intEnterNumberOfTickets * cdecTotalCostOfTickets
Me.lblTotalCostOfTickets.Text = decTotalCostOfTickets.ToString("C")
No comments:
Post a Comment