944,004 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 821
  • VB.NET RSS
Nov 3rd, 2009
0

How to add strings that contain amounts

Expand Post »
vb.net Syntax (Toggle Plain Text)
  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()
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
JRabbit2307 is offline Offline
32 posts
since Oct 2009
Nov 3rd, 2009
1
Re: How to add strings that contain amounts
You need to be storing these individual values into numeric variables to be calculated at the end.

vb.net Syntax (Toggle Plain Text)
  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()
Reputation Points: 84
Solved Threads: 48
Posting Whiz
TomW is offline Offline
342 posts
since Sep 2009
Nov 3rd, 2009
1
Re: How to add strings that contain amounts
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.
Reputation Points: 27
Solved Threads: 17
Junior Poster in Training
mikiurban is offline Offline
63 posts
since Oct 2009
Nov 3rd, 2009
1
Re: How to add strings that contain amounts
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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 3rd, 2009
1
Re: How to add strings that contain amounts
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.
Reputation Points: 27
Solved Threads: 17
Junior Poster in Training
mikiurban is offline Offline
63 posts
since Oct 2009
Nov 5th, 2009
0
Re: How to add strings that contain amounts
Click to Expand / Collapse  Quote originally posted by mikiurban ...
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
Reputation Points: 10
Solved Threads: 0
Light Poster
JRabbit2307 is offline Offline
32 posts
since Oct 2009
Nov 5th, 2009
0
Re: How to add strings that contain amounts
Click to Expand / Collapse  Quote originally posted by sknake ...
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
Reputation Points: 10
Solved Threads: 0
Light Poster
JRabbit2307 is offline Offline
32 posts
since Oct 2009
Nov 5th, 2009
-6
Re: How to add strings that contain amounts
Cast with the "CInt" function
Last edited by jbennet; Nov 5th, 2009 at 6:20 am.
Moderator
Featured Poster
Reputation Points: 1800
Solved Threads: 575
Moderator
jbennet is offline Offline
16,522 posts
since Apr 2005
Nov 5th, 2009
0
Re: How to add strings that contain amounts
Click to Expand / Collapse  Quote originally posted by jbennet ...
Cast with the "CInt" function
jbennet i'm new to programming and what am i supposed to do with "CInt"???
Reputation Points: 10
Solved Threads: 0
Light Poster
JRabbit2307 is offline Offline
32 posts
since Oct 2009
Nov 5th, 2009
-7
Re: How to add strings that contain amounts
CInt casts a string to an integer. e.g.

VB.NET Syntax (Toggle Plain Text)
  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.
Moderator
Featured Poster
Reputation Points: 1800
Solved Threads: 575
Moderator
jbennet is offline Offline
16,522 posts
since Apr 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: My VB app looks blocky when I move to a different OS
Next Thread in VB.NET Forum Timeline: Help with class





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC