Single Coil
New member
- Joined
- May 11, 2008
- Messages
- 3
- Programming Experience
- Beginner
Hi, I am making a miles per gallon program to work out the miles per gallon of a car.
I am nearly there but I keep getting an error in my code:
There are 2 textboxes, one is miles and the other is litres - there is a button which is clicked to calculate the answer in miles per gallon.
I take the users input from the miles textbox and the input from the litres textbox which divides to give the number of miles for 1 litre of petrol - I then multiply this number by 4.55 to get the miles per gallon.
The problem is this:
If the user enters numbers into both textboxes, it calculates it fine but if one or both textboxes are empty and the button is pressed, an error occurs which says "Invalid cast exception - Conversion from string "" to type 'Long' is not valid"
My code for the calculation is below - I have tried to think of what I should do but I don't know what to do - any help would be good, thanks!
I know it's probably simple but I can't think at all
I am nearly there but I keep getting an error in my code:
There are 2 textboxes, one is miles and the other is litres - there is a button which is clicked to calculate the answer in miles per gallon.
I take the users input from the miles textbox and the input from the litres textbox which divides to give the number of miles for 1 litre of petrol - I then multiply this number by 4.55 to get the miles per gallon.
The problem is this:
If the user enters numbers into both textboxes, it calculates it fine but if one or both textboxes are empty and the button is pressed, an error occurs which says "Invalid cast exception - Conversion from string "" to type 'Long' is not valid"
My code for the calculation is below - I have tried to think of what I should do but I don't know what to do - any help would be good, thanks!
VB.NET:
Dim Gall As Single = 4.55
txtResult.Text = ((txtMiles.Text / txtLitres.Text) * Gall)
I know it's probably simple but I can't think at all
Last edited: