How to add strings that contain amounts

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 32
Reputation: JRabbit2307 is an unknown quantity at this point 
Solved Threads: 0
JRabbit2307 JRabbit2307 is offline Offline
Light Poster

How to add strings that contain amounts

 
0
  #1
Nov 3rd, 2009
  1. 'Radio Choice Selection
  2. Console.WriteLine("Please enter the Radio Choice for your vehicle: ")
  3. Console.WriteLine("Enter 1 for AM/FM Radio")
  4. Console.WriteLine("Enter 2 for AM/FM/CD/DVD")
  5. strRadioChoice = Console.ReadLine()
  6. Select Case strRadioChoice
  7. Case "1"
  8. Console.WriteLine("Price : $100")
  9.  
  10. Case "2"
  11. Console.WriteLine("Price : $400")
  12.  
  13. Case Else
  14. Console.WriteLine("Invalid selection")
  15.  
  16. End Select
  17.  
  18. SellingPrice = BasePrice + strEngineChoice + strInteriorChoice + strRadioChoice + ShippingCharge + DealerCharge
  19. Console.WriteLine("The total selling price for your vehicle is $" + SellingPrice)
  20. Console.WriteLine()
  21. Console.WriteLine("Press Enter to Exit")
  22. Console.ReadLine()
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 321
Reputation: TomW is on a distinguished road 
Solved Threads: 45
TomW TomW is offline Offline
Posting Whiz
 
1
  #2
Nov 3rd, 2009
You need to be storing these individual values into numeric variables to be calculated at the end.

Originally Posted by JRabbit2307 View Post
  1. 'Radio Choice
  2. Selection
  3.  
  4. Dim decRadioPrice As Decimal
  5.  
  6. Console.WriteLine("Please enter the Radio Choice for your vehicle: ")
  7. Console.WriteLine("Enter 1 for AM/FM Radio")
  8. Console.WriteLine("Enter 2 for AM/FM/CD/DVD")
  9. strRadioChoice = Console.ReadLine()
  10. Select Case strRadioChoice
  11. Case "1"
  12. Console.WriteLine("Price : $100")
  13. decRadioPrice = 100.00
  14. Case "2"
  15. Console.WriteLine("Price : $400")
  16. decRadioPrice = 400.00
  17. Case Else
  18. Console.WriteLine("Invalid selection")
  19.  
  20. End Select
  21.  
  22. 'SellingPrice = BasePrice + strEngineChoice + strInteriorChoice
  23. '+ strRadioChoice + ShippingCharge + DealerCharge
  24.  
  25. decTotal = decBasePrice + desEnginePrice + decInterior + decRadio etc
  26. Console.WriteLine("The total selling price for your vehicle is $" + FormatCurrencty(decTotal))
  27. Console.WriteLine()
  28. Console.WriteLine("Press Enter to Exit")
  29. Console.ReadLine()
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 63
Reputation: mikiurban is an unknown quantity at this point 
Solved Threads: 17
mikiurban mikiurban is offline Offline
Junior Poster in Training
 
1
  #3
Nov 3rd, 2009
Or you can use Integer.Parse() (or Double.Parse(), etc) at that line where you add all the numbers. Maybe add some exception around it, too, .Parse() is pretty picky and throws exceptions when it doesn't get what it wants.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,473
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 630
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
1
  #4
Nov 3rd, 2009
You should use integer.TryParse() or decimal.TryParse() instead of handling the exception. Throwing exceptions is a very expensive process and if you have exceptions being thrown in calculations (where it can occur over and over in loops) it can bring a system to a halt depending on the amount of data.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 63
Reputation: mikiurban is an unknown quantity at this point 
Solved Threads: 17
mikiurban mikiurban is offline Offline
Junior Poster in Training
 
1
  #5
Nov 3rd, 2009
TryParse is much better than Parse (unless you want exceptions I suppose) . My current job is in .Net 1.1, and I don't get to use TryParse, so I forgot about it. Yes, I do cry a little inside.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 32
Reputation: JRabbit2307 is an unknown quantity at this point 
Solved Threads: 0
JRabbit2307 JRabbit2307 is offline Offline
Light Poster
 
0
  #6
Nov 5th, 2009
Originally Posted by mikiurban View Post
Or you can use Integer.Parse() (or Double.Parse(), etc) at that line where you add all the numbers. Maybe add some exception around it, too, .Parse() is pretty picky and throws exceptions when it doesn't get what it wants.

mikiurban are you saying to do decTotal = decBasePrice + decEnginePrice + decInterior + decRadio + decShippingCharge + decDealerCharge
Console.WriteLine("The total selling price for your vehicle is $" + Integer.Parse(decTotal))
I tried integer.parse() and parse.() neither worked
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 32
Reputation: JRabbit2307 is an unknown quantity at this point 
Solved Threads: 0
JRabbit2307 JRabbit2307 is offline Offline
Light Poster
 
0
  #7
Nov 5th, 2009
Originally Posted by sknake View Post
You should use integer.TryParse() or decimal.TryParse() instead of handling the exception. Throwing exceptions is a very expensive process and if you have exceptions being thrown in calculations (where it can occur over and over in loops) it can bring a system to a halt depending on the amount of data.
sknake i tried that and its saying "overload resolution failed because no accessible 'TryParse' accepts this number of arguments
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,273
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 544
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator
 
-6
  #8
Nov 5th, 2009
Cast with the "CInt" function
Last edited by jbennet; Nov 5th, 2009 at 6:20 am.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 32
Reputation: JRabbit2307 is an unknown quantity at this point 
Solved Threads: 0
JRabbit2307 JRabbit2307 is offline Offline
Light Poster
 
0
  #9
Nov 5th, 2009
Originally Posted by jbennet View Post
Cast with the "CInt" function
jbennet i'm new to programming and what am i supposed to do with "CInt"???
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 16,273
Reputation: jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all jbennet is a name known to all 
Solved Threads: 544
Moderator
Featured Poster
jbennet's Avatar
jbennet jbennet is offline Offline
Moderator
 
-7
  #10
Nov 5th, 2009
CInt casts a string to an integer. e.g.

  1. Dim answer As Integer
  2. answer = CInt(TextBox1.Text) + CInt(TextBox2.Text)

Will add two strings (the data in the textboxes) and store the result in an integer variable called answer.

Look at:
http://www.vbdotnetforums.com/vb-net...pose-cint.html
Last edited by jbennet; Nov 5th, 2009 at 6:35 am.
If i am helpful, please give me reputation points.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 456 | Replies: 9
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC