By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,954 Members | 1,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,954 IT Pros & Developers. It's quick & easy.

Additional information: Cast from string "" to type 'Double' is not valid. error

P: n/a
For an intro to VB project I have to write a program which takes an
amount of Miles, Yards, and Inches.....and converts it to metric (KM,
M, and CM) when all values are entered to the input text boxes it works
fine, but if there is a number missing I get the following error:

An unhandled exception of type 'System.InvalidCastException' occurred
in microsoft.visualbasic.dll

Additional information: Cast from string "" to type 'Double' is not
valid.

i have already tried a

If txtmiles.Text = "" Then
txmiles.Text = "0"
End If and still didnt work

here is my code if anyone can help thatd be great

Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCompute.Click

'This string takes the inputted numbers and converts them all
to inches.
Dim inches As String
inches = (txtMiles.Text) * 63360 + (txtYards.Text) * 36 +
(txtFeet.Text) * 12 + (txtInches.Text)

'This part converts CStr(inches) which was calculated above
into Meters
Dim meters As String
meters = CStr(inches) / 39.37

'This calculates the calculates the kilometers, meters, and
centimeters.
Dim Kilometers As String
Kilometers = CStr(meters) / 1000
Int(CStr(Kilometers))

Dim OutputMeters As String
OutputMeters = ((CStr(Kilometers) - Int(CStr(Kilometers))) * 10
^ 3)
Int(CStr(OutputMeters))

Dim Centimeters As String
Centimeters = ((CStr(OutputMeters) - Int(CStr(OutputMeters))) *
10 ^ 2)

'This posts the results to the results boxes
txtKilometers.Text = Int(CStr(Kilometers))
txtmeters.Text = Int(CStr(OutputMeters))
'This will round the centimeters to the decimal place and post
it
Dim n As Double = CStr(Centimeters)
txtCentimeters.Text = (Math.Round(n, 1))

'This will disable the txt box and enable the results
txtMiles.ReadOnly = True
txtYards.ReadOnly = True
txtFeet.ReadOnly = True
txtInches.ReadOnly = True
txtmeters.ReadOnly = False
txtCentimeters.ReadOnly = False
txtKilometers.ReadOnly = False

End Sub

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnReset.Click
'This button will reset and clear all the text boxes
txtMiles.Clear()
txtYards.Clear()
txtFeet.Clear()
txtInches.Clear()
txtKilometers.Clear()
txtmeters.Clear()
txtCentimeters.Clear()
'This will enable the txt box and disable the results
txtMiles.ReadOnly = False
txtYards.ReadOnly = False
txtFeet.ReadOnly = False
txtInches.ReadOnly = False
txtKilometers.ReadOnly = True
txtmeters.ReadOnly = True
txtCentimeters.ReadOnly = True

End Sub
End Class

Feb 4 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
DC15,

I can see that you don't have in top of your program Option Strict On, I
think that surely for this program you need that.

Net is calculating with most operators with doubles.

Therefore does it look for me better to take the double as the internal
value in this situation.

You can convert a string to double with simple CDbl(String)

I hope this helps,

Cor

"dc15" <de******@gmail.com> schreef in bericht
news:11********************@g44g2000cwa.googlegrou ps.com...
For an intro to VB project I have to write a program which takes an
amount of Miles, Yards, and Inches.....and converts it to metric (KM,
M, and CM) when all values are entered to the input text boxes it works
fine, but if there is a number missing I get the following error:

An unhandled exception of type 'System.InvalidCastException' occurred
in microsoft.visualbasic.dll

Additional information: Cast from string "" to type 'Double' is not
valid.

i have already tried a

If txtmiles.Text = "" Then
txmiles.Text = "0"
End If and still didnt work

here is my code if anyone can help thatd be great

Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCompute.Click

'This string takes the inputted numbers and converts them all
to inches.
Dim inches As String
inches = (txtMiles.Text) * 63360 + (txtYards.Text) * 36 +
(txtFeet.Text) * 12 + (txtInches.Text)

'This part converts CStr(inches) which was calculated above
into Meters
Dim meters As String
meters = CStr(inches) / 39.37

'This calculates the calculates the kilometers, meters, and
centimeters.
Dim Kilometers As String
Kilometers = CStr(meters) / 1000
Int(CStr(Kilometers))

Dim OutputMeters As String
OutputMeters = ((CStr(Kilometers) - Int(CStr(Kilometers))) * 10
^ 3)
Int(CStr(OutputMeters))

Dim Centimeters As String
Centimeters = ((CStr(OutputMeters) - Int(CStr(OutputMeters))) *
10 ^ 2)

'This posts the results to the results boxes
txtKilometers.Text = Int(CStr(Kilometers))
txtmeters.Text = Int(CStr(OutputMeters))
'This will round the centimeters to the decimal place and post
it
Dim n As Double = CStr(Centimeters)
txtCentimeters.Text = (Math.Round(n, 1))

'This will disable the txt box and enable the results
txtMiles.ReadOnly = True
txtYards.ReadOnly = True
txtFeet.ReadOnly = True
txtInches.ReadOnly = True
txtmeters.ReadOnly = False
txtCentimeters.ReadOnly = False
txtKilometers.ReadOnly = False

End Sub

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnReset.Click
'This button will reset and clear all the text boxes
txtMiles.Clear()
txtYards.Clear()
txtFeet.Clear()
txtInches.Clear()
txtKilometers.Clear()
txtmeters.Clear()
txtCentimeters.Clear()
'This will enable the txt box and disable the results
txtMiles.ReadOnly = False
txtYards.ReadOnly = False
txtFeet.ReadOnly = False
txtInches.ReadOnly = False
txtKilometers.ReadOnly = True
txtmeters.ReadOnly = True
txtCentimeters.ReadOnly = True

End Sub
End Class

Feb 4 '06 #2

P: n/a
"dc15" <de******@gmail.com> schrieb:
For an intro to VB project I have to write a program which takes an
amount of Miles, Yards, and Inches.....and converts it to metric (KM,
M, and CM) when all values are entered to the input text boxes it works
fine, but if there is a number missing I get the following error:

An unhandled exception of type 'System.InvalidCastException' occurred
in microsoft.visualbasic.dll

Additional information: Cast from string "" to type 'Double' is not
valid.


Turn on 'Option Strict'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Feb 4 '06 #3

P: n/a
Thanks for all the help, i'd always thought Option Strict On was a
default setting but I guess I was wrong. I solved the first problem by
using an If Statement, but now im running into a whole new problem.
When I enabled option strict I got an error "

"Option Strict On disallows implicit conversions from 'Double' to
'String'."
on the part posting the output to the text boxes.... here is my code

ption Strict On

Public Class Form1
Inherits System.Windows.Forms.Form


" Windows Form Designer generated code "

Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnCompute.Click

'These IF statements will prevent an error from occuring if
there is no value in the text boxes
If txtMiles.Text.Trim() = "" Then
txtMiles.Text = "0"
End If
If txtYards.Text.Trim() = "" Then
txtYards.Text = "0"
End If
If txtInches.Text.Trim() = "" Then
txtInches.Text = "0"
End If
If txtFeet.Text.Trim() = "" Then
txtFeet.Text = "0"
End If

'This Double takes the inputted numbers and converts them all
to inches.
Dim inches As Double
inches = CDbl(txtMiles.Text) * 63360 + CDbl(txtYards.Text) * 36
+ CDbl(txtFeet.Text) * 12 + CDbl(txtInches.Text)
'This part converts Cdbl(inches) which was calculated above
into Meters
Dim meters As Double
meters = CDbl(inches) / 39.37

'This calculates the calculates the kilometers, meters, and
centimeters.
Dim Kilometers As Double
Kilometers = CDbl(meters) / 1000
Int(CDbl(Kilometers))

Dim OutputMeters As Double
OutputMeters = ((CDbl(Kilometers) - Int(CDbl(Kilometers))) * 10
^ 3)
Int(CDbl(OutputMeters))

Dim Centimeters As Double
Centimeters = ((CDbl(OutputMeters) - Int(CDbl(OutputMeters))) *
10 ^ 2)

'This posts the results to the results boxes
txtKilometers.Text = Int(CDbl(Kilometers))

txtmeters.Text = Int(CDbl(OutputMeters))
'This will round the centimeters to the decimal place and post
it
Dim n As Double = CDbl(Centimeters)
n = CDbl(Centimeters)
txtCentimeters.Text = Math.Round(n, 1)

'This will disable the txt box and enable the results
txtMiles.ReadOnly = True
txtYards.ReadOnly = True
txtFeet.ReadOnly = True
txtInches.ReadOnly = True
txtmeters.ReadOnly = False
txtCentimeters.ReadOnly = False
txtKilometers.ReadOnly = False

If txtMiles.Text = "" Then
txtMiles.Text = "0"
End If
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnReset.Click
'This button will reset and clear all the text boxes
txtMiles.Clear()
txtYards.Clear()
txtFeet.Clear()
txtInches.Clear()
txtKilometers.Clear()
txtmeters.Clear()
txtCentimeters.Clear()
'This will enable the txt box and disable the results
txtMiles.ReadOnly = False
txtYards.ReadOnly = False
txtFeet.ReadOnly = False
txtInches.ReadOnly = False
txtKilometers.ReadOnly = True
txtmeters.ReadOnly = True
txtCentimeters.ReadOnly = True

End Sub
End Class
I've tried everything I can, and this is really annoying me. Does
anyone have any ideas on how i can fix this? itd be greatly appreciated

Feb 9 '06 #4

P: n/a
Hello dc15,
Thanks for all the help, i'd always thought Option Strict On was a
default setting but I guess I was wrong. I solved the first problem
by using an If Statement, but now im running into a whole new problem.
When I enabled option strict I got an error "

"Option Strict On disallows implicit conversions from 'Double' to
'String'." <SNIP> txtCentimeters.Text = Math.Round(n, 1)

How about txtCentimeters.Text=n.ToString("n1"). Likewise you could use txtCentimeters.Text=Math.Round(n,1).ToString.
(Beware you will get rounding differences between these options. Math.Round
uses financial rounding whereas the n.ToString("n1") always rounds .5 up.

Additionally, you may want to work more on your type checking. What happens
in your solution when you type "a" in one of the text boxes? You might want
to look into Decimal.TryParse if you are using 2005.

Jim Wooley
Feb 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.