5 Replies - 11013 Views - Last Post: 18 October 2008 - 03:59 AM Rate Topic: -----

#1 arobu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-October 08

from string to decimal

Post icon  Posted 16 October 2008 - 02:41 PM

I need help. I am working on an assignment where the user input needs to be use to calculate. When I run the program it works as long as the price get override otherwise I get an error that says conversion from string to type decimal is not valid. I do not understand why. Here is what i have
Dim decCost As Decimal
		Dim qty As Integer
		Dim override As Decimal
		Dim purchase As Decimal

		'calculate the purchase
		decCost = CDec(lstCost.Items(lstCost.SelectedIndex))
		qty = qtyTextBox.Text
		override = overrideTextBox.Text
		If overrideTextBox.Text = " " Then
			purchase = decCost * qty
		Else
			purchase = override * qty
		End If

		lblTotal.Text = purchase


Thank you for any help

arobu

Is This A Good Question/Topic? 0
  • +

Replies To: from string to decimal

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,410
  • Joined: 29-May 08

Re: from string to decimal

Posted 16 October 2008 - 02:52 PM

View Postarobu, on 16 Oct, 2008 - 10:41 PM, said:

I need help. I am working on an assignment where the user input needs to be use to calculate. When I run the program it works as long as the price get override otherwise I get an error that says conversion from string to type decimal is not valid. I do not understand why. Here is what i have
Dim decCost As Decimal
		Dim qty As Integer
		Dim override As Decimal
		Dim purchase As Decimal

		'calculate the purchase
		decCost = CDec(lstCost.Items(lstCost.SelectedIndex))
		qty = qtyTextBox.Text
		override = overrideTextBox.Text
		If overrideTextBox.Text = " " Then
			purchase = decCost * qty
		Else
			purchase = override * qty
		End If

		lblTotal.Text = purchase


Thank you for any help

arobu


The problem is this
What you think you've type into a text box is a bunch of numbers but the computer interepts it as a string of characters.

So need to convert (or cast) that string into a number

Dim MyNumber As Double
' Edit:
' If the Text can be converted stick it in the MyNumber variable and Return True
' If it can't MyNumber will be the default value (0.00) and Return False
If Double.TryParse("1x3.45", MyNumber ) = False Then
' Can not be converted to a number
End If


This post has been edited by AdamSpeight2008: 16 October 2008 - 03:23 PM

Was This Post Helpful? 1
  • +
  • -

#3 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: from string to decimal

Posted 16 October 2008 - 03:07 PM

I believe this code should work for you:
        Dim decCost As Decimal
        Dim qty As Integer
        Dim override As Decimal
        Dim purchase As Decimal

        'calculate the purchase
        ' Note: i changed the "CDec" to "decimal.Parse"
        decCost = Decimal.Parse(lstCost.Items(lstCost.SelectedIndex)))
        qty = Integer.Parse(qtyTextBox.Text)
        override = Decimal.Parse(overrideTextBox.Text)
        If overrideTextBox.Text = " " Then
            purchase = decCost * qty
        Else
            purchase = override * qty
        End If

        lblTotal.Text = purchase.ToString()


Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,410
  • Joined: 29-May 08

Re: from string to decimal

Posted 16 October 2008 - 03:20 PM

View Postjacobjordan, on 16 Oct, 2008 - 11:07 PM, said:

I believe this code should work for you:
        Dim decCost As Decimal
        Dim qty As Integer
        Dim override As Decimal
        Dim purchase As Decimal

        'calculate the purchase
        ' Note: i changed the "CDec" to "decimal.Parse"
        decCost = Decimal.Parse(lstCost.Items(lstCost.SelectedIndex)))
        qty = Integer.Parse(qtyTextBox.Text)
        override = Decimal.Parse(overrideTextBox.Text)
        If overrideTextBox.Text = " " Then
            purchase = decCost * qty
        Else
            purchase = override * qty
        End If

        lblTotal.Text = purchase.ToString()



This would throw an exception when the user enters something that can't be converted.
It's good practice to make sure the your inputs are valid prior to using them.
Was This Post Helpful? 0
  • +
  • -

#5 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: from string to decimal

Posted 16 October 2008 - 04:03 PM

View PostAdamSpeight2008, on 16 Oct, 2008 - 05:20 PM, said:

View Postjacobjordan, on 16 Oct, 2008 - 11:07 PM, said:

I believe this code should work for you:
        Dim decCost As Decimal
        Dim qty As Integer
        Dim override As Decimal
        Dim purchase As Decimal

        'calculate the purchase
        ' Note: i changed the "CDec" to "decimal.Parse"
        decCost = Decimal.Parse(lstCost.Items(lstCost.SelectedIndex)))
        qty = Integer.Parse(qtyTextBox.Text)
        override = Decimal.Parse(overrideTextBox.Text)
        If overrideTextBox.Text = " " Then
            purchase = decCost * qty
        Else
            purchase = override * qty
        End If

        lblTotal.Text = purchase.ToString()



This would throw an exception when the user enters something that can't be converted.
It's good practice to make sure the your inputs are valid prior to using them.

You are correct.

In that case, you could do one of two things: Put the code above in a try-catch block like
Try
    'Put that code here
Catch
    MessageBox.Show("The input you provided is not a valid number", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try


or you could do what Adam said and put an if-then statement that would only execute you code if the numbers could be converted.
Was This Post Helpful? 1
  • +
  • -

#6 arobu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-October 08

Re: from string to decimal

Posted 18 October 2008 - 03:59 AM

Thank you so much for all your help
Arobu
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1