Results 1 to 4 of 4

Thread: HELP please.

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    2

    HELP please.

    Hey guys,

    I'm sure this is in the wrong section, I don't know where to put it, I'm sorry.

    So I'm making program that calculates the cost of a call based on data inputted, but I am have a major problem, I keep getting an error saying "Conversion from string "" to type 'Double' is not valid."

    I have been searching for days and I understand the problem, that I cannot convert an empty string to a double, but what do I do?

    Here is the code:
    Code:
    Public Class Form1
    
        Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblAccountLabel.Click
    
        End Sub
    
        Private Sub Label1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblTelephoneLabel.Click
    
        End Sub
    
        Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblBasic.Click
    
        End Sub
    
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
            Dim iAccount As Integer
            Dim sTelephone As String
            Dim sPlan As String
            Dim iDuration As Integer
            Dim dDate As Date
            Dim dCountry As Double
            Dim iTime As Integer
            Dim bEvening As Boolean
            Dim dFlagfall As Double
            Dim dBasic As Double
            Dim dPlanDisc As Double
            Dim dLoyalty As Double
            Dim dTotal As Double
            Dim dGST As Double
    
            'Determines Flagfall by inputted plan
            sPlan = txtPlan.Text
            
            dFlagfall = CDbl(lblFlagfall.Text) <-- IT POINTS TO THIS WITH THE ERROR
    
            If sPlan = "No Plan" Then
                dFlagfall = 2.0
            ElseIf sPlan = "Bronze" Then
                dFlagfall = 1.5
            ElseIf sPlan = "Silver" Then
                dFlagfall = 0.75
            ElseIf sPlan = "Gold" Then
                dFlagfall = 0.25
            Else
                MsgBox("Unknown Plan")
            End If
    
            'Calculates rate per minute by inputted country
            dCountry = CDbl(lblCountry.Text)
    
            If txtCountry.Text = "Japan" Then
                dCountry = 0.34
            ElseIf txtCountry.Text = "China" Then
                dCountry = 0.86
            ElseIf txtCountry.Text = "India" Then
                dCountry = 0.78
            ElseIf txtCountry.Text = "UK" Then
                dCountry = 0.37
            ElseIf txtCountry.Text = "USA" Then
                dCountry = 0.42
            ElseIf txtCountry.Text = "Canada" Then
                dCountry = 0.56
            ElseIf txtCountry.Text = "Indonesia" Then
                dCountry = 0.38
            ElseIf txtCountry.Text = "New Zealand" Then
                dCountry = 0.22
            ElseIf txtCountry.Text = "Other" Then
                dCountry = 1.1
            End If
    
            'Displays inputted plan for summary reasons
            lblPlan.Text = sPlan
    
            'Calculates basic cost then displays
            txtDuration.Text = lblDuration.Text
            iDuration = lblDuration.Text
            dBasic = lblBasic.Text
    
            dBasic = dFlagfall + (iDuration * dCountry)
    
            'Displays Plan Discount
            dPlanDisc = CDbl(lblPlanDiscount.Text)
            If sPlan = "No Plan" Then
                dPlanDisc = 0
            ElseIf sPlan = "Bronze" Then
                dPlanDisc = 0.05
            ElseIf sPlan = "Silver" Then
                dPlanDisc = 0.1
            ElseIf sPlan = "Gold" Then
                dPlanDisc = 0.15
            Else
                MsgBox("Unknown Plan")
    
            End If
    
            'Calculates and displays Loyalty discount
            dLoyalty = CDbl(lblLoyalty.Text)
    
            dLoyalty = iTime * 0.02
    
            'Calculates and displays total cost
    
    
    
    
        End Sub
    
        Private Sub lblFlagfall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblFlagfall.Click
    
        End Sub
    End Class
    Any help would be so greatly appreciated and hopefully mods move this to it's correct location.

    Thank you.

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

    Re: HELP please.

    That means that you haven't entered anything into the TextBox, so it's Text property is an empty string. Obviously an empty string isn't a number, so the conversion to a number fails. You should be validating the input to make sure it is a number before using it. Your best bet is to use Double.TryParse, which will return True or False to indicate whether the input was valid, plus pass back the converted value if it was valid. There are lots of examples around.

    By the way, please provide meaningful thread titles in future. The title is supposed to9 tell us what the thread is about so that we don;t have to open every single one to find those that are relevant.
    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

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2011
    Posts
    2

    Re: HELP please.

    I am terribly sorry for the thread heading.

    Thank you for the help though, it got me further but I am still having trouble.

    Should the code look like this?

    Code:
            sPlan = txtPlan.Text
            dFlagfall = lblFlagfall.Text
    
            If Not Double.TryParse(lblFlagfall.Text, dFlagfall) Then
                MsgBox("Error")
            ElseIf sPlan = "No Plan" Then
                dFlagfall = 2.0
            ElseIf sPlan = "Bronze" Then
                lblFlagfall.Text = CDbl(1.5)
            ElseIf sPlan = "Silver" Then
                lblFlagfall.Text = CDbl(0.75)
            ElseIf sPlan = "Gold" Then
                lblFlagfall.Text = CDbl(0.25)
            Else
                MsgBox("Unknown Plan")
            End If
    Thanks again.

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

    Re: HELP please.

    The point of TryParse is to try to parse the user input and, if successful, give you the result. Where is the user input? Presumably it's not in lblFlagfall because the user can't enter data into a Label.

    Presumably the user is entering the data into a TextBox, so it should be the Text of that TextBox you pass to TryParse. You should also be passing a Double variable that you want to contain the result of the conversion. If the conversion is successful then TryParse returns True and that variable contains the result, so you can use that variable. If the conversion fails then TryParse returns False and the variable contains zero, so you should not use the variable unless you specifically want to use zero when invalid data is entered.

    Here's an example of how you might use Double.TryParse:
    vb.net Code:
    1. Const HOURLY_RATE As Double = 10.0
    2. Dim hoursWorked As Double
    3.  
    4. If Double.TryParse(hoursWorkedTextBox.Text, hoursWorked) Then
    5.     Dim totalPay As Double = hoursWorked * HOURLY_RATE
    6.  
    7.     MessageBox.Show(totalPay.ToString("c"), "Total Pay")
    8. Else
    9.     MessageBox.Show("Please enter a valid number of hours.")
    10. End If
    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