Results 1 to 3 of 3

Thread: [2005] Newbie Help String & Double Trouble

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    1

    [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:
    1. Public Class business
    2.  
    3.     Dim fmtStrg As String = "{0, -19}{1, 12:C}{2, 12:C}{3, 12:C}{4, 12:C}"
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         lstBox.Items.Add(String.Format(fmtStrg, "Item", "Cost", "Rebate", "Sold", "Profit"))
    7.         lstBox.Items.Add(String.Format(fmtStrg, "-------------------", "-----------", "-----------", "-----------", "-----------"))
    8.     End Sub
    9.  
    10.     Private Sub txtAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
    11.         Dim Item As String
    12.         Dim Cost, Rebate, Sold, Profit As Double
    13.  
    14.         If txtItem.Text = "" Then
    15.             MsgBox("Cannot have empty item field.", 0, "Error")
    16.         End If
    17.  
    18.         Item = CStr(txtItem.Text)
    19.         Cost = CDbl(txtCost.Text)
    20.         Sold = CDbl(txtSold.Text)
    21.         Rebate = CDbl(txtRebate.Text)
    22.  
    23.         Profit = (Sold + Rebate) - Cost
    24.  
    25.         lstBox.Items.Add(String.Format(fmtStrg, Item, Cost, Rebate, Sold, Profit))
    26.         txtTotalProfit.Text = FormatCurrency(Profit + CDbl(txtTotalProfit.Text))
    27.  
    28.         txtCost.Clear()
    29.         txtRebate.Text = 0
    30.         txtSold.Clear()
    31.         txtItem.Clear()
    32.  
    33.         txtItem.Focus()
    34.     End Sub
    35.     Private Sub txtSold_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSold.TextChanged
    36.         Dim Cost, Rebate, Sold As Double
    37.  
    38.         Cost = CDbl(txtCost.Text)
    39.         Sold = CDbl(txtSold.Text)
    40.         Rebate = CDbl(txtRebate.Text)
    41.  
    42.         txtProfit.Text = CStr((Sold + Rebate) - Cost)
    43.     End Sub
    44. End Class

    Thank You.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim obj As Object = "Hello World"
    2. 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:
    1. txtProfit.Text = (Sold + Rebate - Cost).ToString()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width