Reputation: 97
I keep getting this annoying message (even though it is my fault):
Conversion from string "" to type 'Double' is not valid.
I get this message when I am debugging in VB 2010 Express and I hit the "SLM" button. What the heck does this mean and what am I doing wrong?!?!
My code:
Public Class frmAssignment_5
Private Sub btnSLM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSLM.Click
Dim Description As String
Dim Year As String
Dim Life As String
Dim SLMDepreciation As String
Dim SLMText As String = "straight-line"
txtDescription.Clear()
txtYear.Clear()
txtCost.Clear()
txtLife.Clear()
lstResult.ClearSelected()
Description = CStr(txtDescription.Text)
Year = CStr(txtYear.Text)
Life = CStr(txtLife.Text)
SLMDepreciation = CStr(SLMD())
lstResult.Items.Add("Description: " & Description)
lstResult.Items.Add("Year of purchase: " & Year)
lstResult.Items.Add("Cost: " & FormatCurrency(CStr(txtCost.Text), 2))
lstResult.Items.Add("Estimated life: " & Life)
lstResult.Items.Add("Method of depreciation: " & SLMText)
End Sub
Function SLMD() As Double
Dim Salvage_Value As Integer = 0
Dim Life As String = CStr(txtLife.Text)
Dim Cost As String = CStr(txtCost.Text)
SLMD = (CDbl(txtCost.Text) - Salvage_Value) / CDbl(Life)
Return SLMD
End Function
End Class
The error keeps happening at the "SLMD = (CDbl(txtCost.Text) - Salvage_Value) / CDbl(Life)" but when I change things around, it starts pointing at the things that I change with the same error.
If anyone can help me out, I will be in forever debt...
Upvotes: 0
Views: 11176
Reputation: 5393
VB is complaining because you are trying to convert an empty String "" value to a number. If you put VAL() around each string, it will convert it to zero.
Salvage_Value is also declared as an Integer, but you are using it as a Double, so you need to convert that too when calculating SLMD:
SLMD = (Val(txtCost.Text) - CDbl(Salvage_Value)) / Val(Life)
You also have a risk of a division by zero error if "Life" is not set to a value, so you should add code to make sure a value is entered there.
Upvotes: 4
Reputation: 54532
This is very similar to a question I just answered. When you use CDbl to convert a string it will error out if the conversion is not possible, what I would do is use Double.TryParse and set the values individually, then if there was an error I would handle it.
Function SLMD() As Double
Dim Salvage_Value As Integer = 0
Dim Life As Double
Dim Cost As Double
If Not Double.TryParse(txtLife.Text, Life) Then
'Do something to handle error
End If
If Not Double.TryParse(txtCost.Text, Cost) Then
'Do something to handle error
End If
Return (Cost - Salvage_Value) / Life
End Function
Upvotes: 3
Reputation: 14747
It looks to me like you're changing trying to convert an empty String
(""
) to a Double
at CDbl(txtCost.Text)
. This is not a valid conversion because "" is not a valid
Double
value.
I think you should be checking your txtCost.Text
to see if it's actually a Double
represented as a String
before attempting the conversion. You could try to block invalid characters/values from the client or check it from the server after you receive it (but before you attempt the conversion). Another option would be to handle it in a try+catch+finally statement. This post might help as well with what you're encountering.
Upvotes: 0