8 Replies - 451 Views - Last Post: 09 October 2012 - 09:34 AM Rate Topic: -----

#1 dbhughes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-October 12

conversion from string "" as well as a few other things

Posted 08 October 2012 - 03:36 PM

I'm having this simple problem. I have written out the code, which makes sense to me, yet still giving me a conversion error from string to double '' at the txtLabor section. I'm unsure why. The program is a simple check and txt box program that results in pricing. I looked into the conversion issue, but am not the best programmer and couldn't figure out how to apply examples to my situation. Any advice would be greatly appreciated. I'm also aware of the unchecking redundancy.. I'm just concerned with the calculation portion.


Posted Image

Public Class Form1

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        chkOilChange.Checked = False
        chkLubeJob.Checked = False
        chkRadiatorFlush.Checked = False
        chkTransmissionFlush.Checked = False
        chkInspection.Checked = False
        chkReplaceMuffler.Checked = False
        chkTireRotation.Checked = False
        txtParts.Clear()
        txtLabor.Clear()
        txtServNLabor.Text = String.Empty
        txtTotalParts.Text = String.Empty
        txtTax.Text = String.Empty
        txtTotalFees.Text = String.Empty
    End Sub

    Private Sub BtnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalculate.Click

        Dim ServNLabor As Double = OilLubeCharges() + FlushCharges() + MiscCharges() + GetLabor()
        Dim Labor As Double = txtLabor.Text
        Dim Parts As Double = GetParts()
        Dim Tax As Double = Taxcharges()
        Dim Total As Double = TotalCharges()
        txtParts.Text = Parts.ToString("C")
        txtLabor.Text = Labor.ToString("C")
        txtServNLabor.Text = ServNLabor.ToString("C")
        txtTotalParts.Text = Parts.ToString("C")
        txtTax.Text = Tax.ToString("C")
        txtTotalFees.Text = Total.ToString("C")

    End Sub

    'Cost Constants
    Dim OilChange As Double = 26.0
    Dim LubeJob As Double = 18.0
    Dim RadiatorFlush As Double = 30.0
    Dim TransmissionFlush As Double = 80.0
    Dim Inspection As Double = 15.0
    Dim ReplaceMuffler As Double = 100.0
    Dim TireRotation As Double = 20.0

        'Calculates the charges for the oil and lube group box
    Function OilLubeCharges()
        Dim Charges As Double = 0.0
        If chkOilChange.Checked = True Then
            Charges = Charges + OilChange
        End If
        If chkLubeJob.Checked = True Then
            Charges = Charges + LubeJob
        End If
        Return Charges
    End Function

    'Calculates the charges for the flushes group box
    Function FlushCharges()
        Dim Charges As Double = 0.0
        If chkRadiatorFlush.Checked = True Then
            Charges = Charges + RadiatorFlush
        End If
        If chkTransmissionFlush.Checked = True Then
            Charges = Charges + TransmissionFlush
        End If
        Return Charges
    End Function

    'Calculates the charges for the Misc. group box
    Function MiscCharges()
        Dim Charges As Double = 0.0
        If chkInspection.Checked = True Then
            Charges = Charges + Inspection
        End If
        If chkReplaceMuffler.Checked = True Then
            Charges = Charges + ReplaceMuffler
        End If
        If chkTireRotation.Checked = True Then
            Charges = Charges + TireRotation
        End If
        Return Charges
    End Function

    'Calculates the charges for the parts textbox
    Function GetParts()
        Dim Charges As Double = 0.0
        Try
            Charges = Charges + txtParts.Text
        Catch ex As Exception
            MessageBox.Show("Please enter valid input in Parts")
        End Try
        If Charges <= 0 Then
            Charges = 0.0
        End If
        Return Charges
    End Function

    'Calculates the charges for the labor textbox
    Function GetLabor()
        Dim Charges As Double = 0.0
        If txtLabor.Text = "" Then
            Return Charges
        End If
        Try
            Charges = Charges + txtLabor.Text
        Catch ex As Exception
            MessageBox.Show("Please enter valid input in Labor")
        End Try
        If Charges < 0 Then
            MessageBox.Show("Please do not enter negative values")
            Charges = 0.0
        End If
        Return Charges
    End Function


    'Calculates the charges for labor and parts combined
    Function OtherCharges()
        Dim Charges As Double = 0.0
        Charges = GetLabor() + GetParts()
        Return Charges
    End Function

    'Calculates Tax
    Function Taxcharges()
        Dim Tax As Double = 0.0
        If txtParts.Text = "" Then
            Return Tax
        End If
        Tax = (GetParts() * 0.06)
        Return Tax
    End Function

    'Calculates all charges together
    Function TotalCharges()
        Dim Charges As Double = 0.0
        Charges = OilLubeCharges() + FlushCharges() + MiscCharges() + OtherCharges() + Taxcharges()
        Return Charges
    End Function

    'Clears Group Box 1
    Sub ClearOilLube()
        chkOilChange.Checked = False
        chkLubeJob.Checked = False
    End Sub

    'Clears Group Box 2
    Sub ClearFlushes()
        chkRadiatorFlush.Checked = False
        chkTransmissionFlush.Checked = False
    End Sub

    'Clears Group Box 3
    Sub ClearMisc()
        chkInspection.Checked = False
        chkReplaceMuffler.Checked = False
        chkTireRotation.Checked = False
    End Sub

    'Clears the parts and labor text boxes
    Sub ClearOther()
        txtParts.Text = "$0.00"
        txtLabor.Text = "$0.00"
    End Sub

    'Clears the summary text boxes
    Sub ClearFees()
        txtServNLabor.Text = "$0.00"
        txtTotalParts.Text = "$0.00"
        txtTax.Text = "$0.00"
        txtTotalFees.Text = "$0.00"
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: conversion from string "" as well as a few other things

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: conversion from string "" as well as a few other things

Posted 08 October 2012 - 04:02 PM

Tip: Don't use textboxes for numbers. Textboxes are for text. NumericUpDown is a good GUI tool for number-only input. That way you don't have to validate the user input. They can't enter a price of "Yogi bear" for example.
Was This Post Helpful? 0
  • +
  • -

#3 dbhughes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-October 12

Re: conversion from string "" as well as a few other things

Posted 08 October 2012 - 04:14 PM

View PosttlhIn`toq, on 08 October 2012 - 04:02 PM, said:

Tip: Don't use textboxes for numbers. Textboxes are for text. NumericUpDown is a good GUI tool for number-only input. That way you don't have to validate the user input. They can't enter a price of "Yogi bear" for example.


I only use textboxes as they hold the box in place for an input. Although, this shouldn't be the reason for my errors in this code, am I correct?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: conversion from string "" as well as a few other things

Posted 08 October 2012 - 04:21 PM

View Postdbhughes, on 08 October 2012 - 05:14 PM, said:

I only use textboxes as they hold the box in place for an input.

I have no idea what you're trying to say.

View Postdbhughes, on 08 October 2012 - 05:14 PM, said:

Although, this shouldn't be the reason for my errors in this code, am I correct?

Quote

a conversion error from string to double '' at the txtLabor section.


If you aren't using a textbox then you don't have to convert text to a double. A numbericUpDown doesn't have a .Text property. It uses a .Value property which is already a numeric type (Decimal).

Your error is most likely because you don't check to see if there even is text in the box. You can't convert null text to a value. '0' can be made 0. '5' can be made 5. But what is the numeric equivalent of nothing?
Was This Post Helpful? 1
  • +
  • -

#5 dbhughes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-October 12

Re: conversion from string "" as well as a few other things

Posted 08 October 2012 - 04:34 PM

So you are saying to not dimension it?

This post has been edited by AdamSpeight2008: 08 October 2012 - 04:55 PM

Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5476
  • View blog
  • Posts: 11,762
  • Joined: 02-June 10

Re: conversion from string "" as well as a few other things

Posted 08 October 2012 - 04:41 PM

Please don't think I'm being a smart arse when I ask this because your profile doesn't say: Is English a second language for you? Are you having trouble translating my original advice?

I'm saying, don't use a textbox - use a numericUpDown instead. That way you don't have to convert text to a number... you don't have to validate the user input... The user can't enter anything that isn't a number... Your numericUpdown will always have a value even if it is 0 as the default you set in your designer.
Was This Post Helpful? 0
  • +
  • -

#7 PeterH  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 47
  • Joined: 03-September 09

Re: conversion from string "" as well as a few other things

Posted 09 October 2012 - 06:54 AM

You need to 'convert' the string representation of a value held in the .text property, to the appropriate target data type, in your case, this is a double, hence,

 Dim Labor As Double = cdbl(txtLabor.Text) 


This should also be the case for any other string to numeric assignments you make. As previously explained by using the NumericUpdown control, this would not be required, as it is already defined as a numeric value.
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: conversion from string "" as well as a few other things

Posted 09 October 2012 - 07:08 AM

Plus using a NUD control as what I think tlhIn`toq is trying to point out will not let the user type something that will cause cdbl() to throw an exception. For example, what keeps the user from typing "Hello World" in the labor textbox?
Was This Post Helpful? 0
  • +
  • -

#9 torind_2000  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 57
  • View blog
  • Posts: 288
  • Joined: 22-August 11

Re: conversion from string "" as well as a few other things

Posted 09 October 2012 - 09:34 AM

View Postdbhughes, on 08 October 2012 - 06:36 PM, said:

    Private Sub BtnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalculate.Click

        Dim Labor As Double = txtLabor.Text  'this is a double variable and you assign it a string value. probably should use the .TryParse method on the txtLabor.text field.

   txtParts.Text = Parts.ToString("C")           'Not sure why you do this here in these next few lines, they are already strings
   txtLabor.Text = Labor.ToString("C")           '.ToString is usually used to convert numbers to string values
   txtServNLabor.Text = ServNLabor.ToString("C") ' so that you can put them in labels or text boxes and stuff
   txtTotalParts.Text = Parts.ToString("C")
   txtTax.Text = Tax.ToString("C")
   txtTotalFees.Text = Total.ToString("C")

    End Sub



View Postdbhughes, on 08 October 2012 - 06:36 PM, said:

    'Cost Constants
    Const OilChange As Double = 26.0
' 
' dim is ok to use, but const is probably better for constants.



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1