Casting fron String to Decimal?

R

Rob Bradford

All.

I'm fairly new to VB.NET, can someone tell me why yhis fails to cast
properly? When Calc_Mileage is called I get he message;

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

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

The text boxes are keystroke trapped to allow only numeric input plus
one "." character

The aim is to add the three figures togetrher display them in a text
box (this may /may not be enabled for input) and stort the value also
as a decimal.

Any help would be appreciated.


Rob.B.

Code snippet.

Dim End_Mileage As Decimal


Private Sub Calc_Mileage()
End_Mileage = CDec(Tb_Start_M.Text) + CDec(Tb_Business_M.Text)
+ CDec(Tb_Private_M.Text)
Tb_End_M.Text = CStr(End_Mileage)
Return
End Sub
 
G

Greg Burns

Looks like one of your text boxes is empty when the procedure is called. It
appears CDec cannot parse "" to 0.

Try

End_Mileage = CDec(Val(Tb_Start_M.Text)) + CDec(Val(Tb_Business_M.Text))
+ CDec(Val(Tb_Private_M.Text))

And dump the Return statement. It is not required for a procedure.

Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top