0

I am trying to do a project that will calculate the weekly pay for employees at an Amusement Park.

When I run it and put numbers in the text boxes I get an error saying "Conversion from String to type Double is not valid".

Can someone kindly explain where I went wrong?

Dim ticketCollection As Double
Dim foodService As Double
Dim cleaningService As Double
Dim ridingAssistance As Double
Dim totalAmount As Double
Dim totalCollectionPay As Double
Dim totalFoodPay As Double
Dim totalCleaningPay As Double
Dim totalRidingPay As Double

ticketCollection = txtCollection.Text
foodService = txtFood.Text
cleaningService = txtCleaning.Text
ridingAssistance = txtRide.Text

totalCollectionPay = ticketCollection * 5
totalFoodPay = foodService * 10
totalCleaningPay = cleaningService * 6
totalRidingPay = ridingAssistance * 5

totalAmount = totalCollectionPay + totalFoodPay + totalCleaningPay + totalRidingPay

lblTotalDue.Text = totalAmount
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • Which line does it occur on? This might be better (simpler) built in Excel. Basically one of your text fields has a non numeric character in it (maybe a blank). You could try this method to ensure that only numbers can be entered: http://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers – Nick.Mc Oct 04 '16 at 02:27
  • @Nick.McDermaid the error is showing on foodservice = txtFood.Text – Romario Alleyne Oct 04 '16 at 02:31
  • 3
    @RomarioAlleyne - You should put `Option Strict On` at the top of your code and see what the compiler then tells you. You really should do that before asking any questions here about VB.NET. – Enigmativity Oct 04 '16 at 03:07

3 Answers3

1

I think it will work...Just try it

Dim ticketCollection As Double
Dim foodService As Double
Dim cleaningService As Double
Dim ridingAssistance As Double
Dim totalAmount As Double
Dim totalCollectionPay As Double
Dim totalFoodPay As Double
Dim totalCleaningPay As Double
Dim totalRidingPay As Double

ticketCollection = val(txtCollection.Text)
foodService = val(txtFood.Text)
cleaningService = val(txtCleaning.Text)
ridingAssistance = val(txtRide.Text)

totalCollectionPay = ticketCollection * 5
totalFoodPay = foodService * 10
totalCleaningPay = cleaningService * 6
totalRidingPay = ridingAssistance * 5

totalAmount = totalCollectionPay + totalFoodPay + totalCleaningPay +      totalRidingPay

lblTotalDue.Text = totalAmount

If you again get any error assign value of totalamount to your label like this,

lblTotalDue.Text = val(totalAmount)
0

Your declarations are all double. When it passes from

.Text

It turns into a string so properly convert it to double first see msdn conversion guide here

P. Pat
  • 488
  • 4
  • 13
0

Try to convert the input into Double first. CDbl converts the input from string type to double. Also put option strict on at top of code to see any implicit conversions happening, this is always good, I would suggest always having this turned on when coding any project. I saw in another answer they are asking to do the same but with val, this might work also but val can have some funny side effects, example. So best not to use val for these reasons. This question also has a good answer explaining this and how you could also try using TryParse method, it's uses integer but you just have to replace it with double. But i would try the below first.

ticketCollection = CDbl(txtCollection.Text)
foodService = CDbl(txtFood.Text)
cleaningService = CDbl(txtCleaning.Text)
ridingAssistance = CDbl(txtRide.Text)

and with this line lblTotalDue.Text = totalAmount you can use lblTotalDue.Text = CStr(totalAmount). CStr converts to string.

Community
  • 1
  • 1
Cal-cium
  • 654
  • 12
  • 22