|
-
Oct 4th, 2006, 08:49 PM
#1
Thread Starter
New Member
[2005] Newbie Help String & Double Trouble
I just started taking VB 2005 in school and decided to make my own program.
Right now you enter Cost, Rebate and Sold into text boxes respectively. When you get to the Sold textbox their is a textChanged Event which displays the profit while your typing it in. When you’re done hit the "Add" button and it will calculate the profit again and display it in the list box.
This is the error I get: "Conversion from string "" to type 'Double' is not valid."
VB Code:
Public Class business
Dim fmtStrg As String = "{0, -19}{1, 12:C}{2, 12:C}{3, 12:C}{4, 12:C}"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lstBox.Items.Add(String.Format(fmtStrg, "Item", "Cost", "Rebate", "Sold", "Profit"))
lstBox.Items.Add(String.Format(fmtStrg, "-------------------", "-----------", "-----------", "-----------", "-----------"))
End Sub
Private Sub txtAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim Item As String
Dim Cost, Rebate, Sold, Profit As Double
If txtItem.Text = "" Then
MsgBox("Cannot have empty item field.", 0, "Error")
End If
Item = CStr(txtItem.Text)
Cost = CDbl(txtCost.Text)
Sold = CDbl(txtSold.Text)
Rebate = CDbl(txtRebate.Text)
Profit = (Sold + Rebate) - Cost
lstBox.Items.Add(String.Format(fmtStrg, Item, Cost, Rebate, Sold, Profit))
txtTotalProfit.Text = FormatCurrency(Profit + CDbl(txtTotalProfit.Text))
txtCost.Clear()
txtRebate.Text = 0
txtSold.Clear()
txtItem.Clear()
txtItem.Focus()
End Sub
Private Sub txtSold_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSold.TextChanged
Dim Cost, Rebate, Sold As Double
Cost = CDbl(txtCost.Text)
Sold = CDbl(txtSold.Text)
Rebate = CDbl(txtRebate.Text)
txtProfit.Text = CStr((Sold + Rebate) - Cost)
End Sub
End Class
Thank You.
-
Oct 4th, 2006, 08:58 PM
#2
Re: [2005] Newbie Help String & Double Trouble
your error occures when one of the textboxes is empty.
when you call CDble() it converts that text to a number, but if the text passed to it is not numeric, the call will fail.
You need to implement some input validation in your application to check the values entered to ensure they will pass through the rest of code without a problem.
-
Oct 4th, 2006, 09:47 PM
#3
Re: [2005] Newbie Help String & Double Trouble
If you're not going to validate input as it's typed then the appropriate way to convert user input to numbers is with the TryParse method of the appropriate type. If the number is an integer then use Integer.TryParse. If it's a double then use Double.TryParse. Note also that Decimal objects are usually used for financial calculations to avoid the round-off errors that can occur with Doubles, although there's unlikely to be an issue with the figures you're dealing with.
Also, the Text property of a control is a String object so the use of CStr is redundant. CStr should only be used to cast a non-string reference to a String abject as String. In English that means that if you have a variable of type Object that refers to a String object then you should use CStr to get a reference to it of type String, e.g.
VB Code:
Dim obj As Object = "Hello World"
Dim str As String = CStr(obj)
If you want to convert an object that is not a String, like a number, to a String then you should call its ToString method, e.g.
VB Code:
txtProfit.Text = (Sold + Rebate - Cost).ToString()
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|