468,167 Members | 1,862 Online

Trouble with conversion of "" in an equation

I have written a simple program that calculates a running \$ total. This
program uses a checkbox to acknowledge that that version of the ticket
is desired and it checks the Qty box to see how many tickets are
desired. Based upon the checkbox being checked and the number of tickets
desired directly affects the running grand total (of course!). OK I
have set where if ticketsDesiredTotal>0 then checkbox.checked = true.
The only problem I have is that if the user deletes the number of
tickets and the user does not enter a number and   is in the tickets
desired box the program crashes. It crashes because it is taking the
value of TicketsDesiredTotal and multiplying it by the per ticket cost.
And the debugger says it cant convert  to use it in the equation.
How can I make this thing work??? Here is the code and the function it
calls:

Function processor()

Dim decTtl As Decimal = 0.0

Dim strOil As String = txtQtyOil.Text
If cbOil.Checked = True Then
lblOilTtl.Text = (txtQtyOil.Text * 150.0)
decTtl += lblOilTtl.Text
End If
If cbAllies.Checked = True Then
lblAlliesTtl.Text = (txtQtyAllies.Text * 50.0)
decTtl += lblAlliesTtl.Text
End If
lblWarTtl.Text = 0.0
decTtl += lblWarTtl.Text
End If
lblSubTotal.Text = String.Format("{0:C}", decTtl)

Dim decTxTtl = CDec(decTtl * 0.07)

lblTaxTtl.Text = String.Format("{0:C}", decTxTtl)
lblGrandTtl.Text = String.Format("{0:C}", (decTxTtl + decTtl))
End Function

Private Sub cbOil_CheckedChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles cbOil.CheckedChanged
processor()
End Sub

Private Sub cbAllies_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cbAllies.CheckedChanged
processor()

End Sub

Private Sub cbWardaddy_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cbWardaddy.CheckedChanged
processor()

End Sub
Mike_Mac

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #1
2 1124
I think you gave us the wrong code or not enough of it. Nonetheless, there
are many different ways of handling an empty string that represents zero.
Two simple methods: either exit the sub when you have an empty string or
convert all empty strings to zero before doing the math. There are endless
permutations of textbox validation of course, including creating an extended
textbox class that has all the validation built-in.

"Michael MacDonald" <ma***********@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I have written a simple program that calculates a running \$ total. This
program uses a checkbox to acknowledge that that version of the ticket
is desired and it checks the Qty box to see how many tickets are
desired. Based upon the checkbox being checked and the number of tickets
desired directly affects the running grand total (of course!). OK I
have set where if ticketsDesiredTotal>0 then checkbox.checked = true.
The only problem I have is that if the user deletes the number of
tickets and the user does not enter a number and " " is in the tickets
desired box the program crashes. It crashes because it is taking the
value of TicketsDesiredTotal and multiplying it by the per ticket cost.
And the debugger says it can't convert "" to use it in the equation.
How can I make this thing work??? Here is the code and the function it
calls:

Function processor()

Dim decTtl As Decimal = 0.0

Dim strOil As String = txtQtyOil.Text
If cbOil.Checked = True Then
lblOilTtl.Text = (txtQtyOil.Text * 150.0)
decTtl += lblOilTtl.Text
End If
If cbAllies.Checked = True Then
lblAlliesTtl.Text = (txtQtyAllies.Text * 50.0)
decTtl += lblAlliesTtl.Text
End If
lblWarTtl.Text = 0.0
decTtl += lblWarTtl.Text
End If
lblSubTotal.Text = String.Format("{0:C}", decTtl)

Dim decTxTtl = CDec(decTtl * 0.07)

lblTaxTtl.Text = String.Format("{0:C}", decTxTtl)
lblGrandTtl.Text = String.Format("{0:C}", (decTxTtl + decTtl))
End Function

Private Sub cbOil_CheckedChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles cbOil.CheckedChanged
processor()
End Sub

Private Sub cbAllies_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cbAllies.CheckedChanged
processor()

End Sub

Private Sub cbWardaddy_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cbWardaddy.CheckedChanged
processor()

End Sub
Mike_Mac

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #2
Hi Michael,

Probabley the most simple thing is to set in top of your program
Option Strict On,

Than you are told that you cannot do a
myString = 1 * 3
There are a lot of simple conversions in Visual Basic which are very handy
and extends very much the single dotNet namespace. By instance Cint (convert
to integer) and Cdbl (convert to double).

However place first that Option Strict On

I hope this helps?

Cor

I have written a simple program that calculates a running \$ total. This
program uses a checkbox to acknowledge that that version of the ticket
is desired and it checks the Qty box to see how many tickets are
desired. Based upon the checkbox being checked and the number of tickets
desired directly affects the running grand total (of course!). OK I
have set where if ticketsDesiredTotal>0 then checkbox.checked = true.
The only problem I have is that if the user deletes the number of
tickets and the user does not enter a number and " " is in the tickets
desired box the program crashes. It crashes because it is taking the
value of TicketsDesiredTotal and multiplying it by the per ticket cost.
And the debugger says it can't convert "" to use it in the equation.
How can I make this thing work??? Here is the code and the function it
calls:

Function processor()

Dim decTtl As Decimal = 0.0

Dim strOil As String = txtQtyOil.Text
If cbOil.Checked = True Then
lblOilTtl.Text = (txtQtyOil.Text * 150.0)
decTtl += lblOilTtl.Text
End If
If cbAllies.Checked = True Then
lblAlliesTtl.Text = (txtQtyAllies.Text * 50.0)
decTtl += lblAlliesTtl.Text
End If
lblWarTtl.Text = 0.0
decTtl += lblWarTtl.Text
End If
lblSubTotal.Text = String.Format("{0:C}", decTtl)

Dim decTxTtl = CDec(decTtl * 0.07)

lblTaxTtl.Text = String.Format("{0:C}", decTxTtl)
lblGrandTtl.Text = String.Format("{0:C}", (decTxTtl + decTtl))
End Function

Private Sub cbOil_CheckedChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles cbOil.CheckedChanged
processor()
End Sub

Private Sub cbAllies_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cbAllies.CheckedChanged
processor()

End Sub

Private Sub cbWardaddy_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles cbWardaddy.CheckedChanged
processor()

End Sub
Mike_Mac

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #3