An unhandled exception of type 'System.InvalidCastException'

Hello all. I’m trying to learn vb.net at home. I have a book with exercises and now I’m stuck because of this error:

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

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

So there is code from a book:

PrivateSub btnCalculate_Click(ByVal senderAs System.Object, _

ByVal eAs System.EventArgs)Handles btnCalculate.Click

Dim dOrderTotalAsDecimal

Dim dDiscountPctAsDecimal

Dim dDiscountAmountAsDecimal

Dim dInvoiceTotalAsDecimal

dOrderTotal = txtOrderTotal.Text

If dOrderTotal >= 100Then

dDiscountPct = 0.2

Else

dDiscountPct = 0

EndIf

dDiscountAmount = dOrderTotal * dDiscountPct

dInvoiceTotal = dOrderTotal - dDiscountAmount

lblDiscountAmount.Text = dDiscountAmount

lblInvoiceTotal.Text = dInvoiceTotal

txtOrderTotal.Focus()

EndSub

If I put let say “233” in Text Box everything is fine, but if I try “233.3” this error occur. I’m using Visual Studio .NET 2003 Service Pack 1. If anybody can explain to me what is wrong and how I can fix it I would really appreciate, because as I mentioned this code is from a book and it supposed to work.

[6419 byte] By [Ovod] at [2007-12-28]
# 1

you are trying to use a value that cannot be used on the decimal object, you should always validate your inputs and catch errors...but this is a talk on a different day.

the problem is, you need to convert the input string to a decimal type! :-) so....

dOrderTotal = Convert.ToDecimal(Me.txtOrderTotal.Text)

ahmedilyas at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

I tried your solution and I get this error:

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Input string was not in a correct format.

On another forum somebody suggest this:

dOrderTotal = Val(txtOrderTotal.Text)

It works, but as I mentioned I’m trying to learn VB at home and I’m really curios why code from a book doesn’t work. There nothing in the book about problem with . character.

Ovod at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

ah! its because you need to convert it to a double value, not decimal..

dOrderTotal = Convert.ToDouble(Me.txtOrderTotal.Text)

ahmedilyas at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

I hardly think

dOrderTotal = Convert.ToDouble(Me.txtOrderTotal.Text)

is an answer when dOrderTotal was declared as decimal.

I suspect the problem is because of location. Are you in a country where the decimal separator is normally , (comma) rather than . (point). Using a . instead of , in such a locale will give the errors described.

Dave299 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
that is true so therefore the variable should be changed to a double type ;-)
ahmedilyas at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6

Ok I changed variable to the Double type and tried this:

dOrderTotal = Convert.ToDouble(Me.txtOrderTotal.Text)

I got this error:

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: Input string was not in a correct format.

Dave299 thanks for your post, I tried “,” instead of “.” end everything works without any conversion. But can anybody explain why Convert.ToDouble didn’t work?

Ovod at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7

Hi.

I'm glad you have got it to work finally.

I don't have VS 2003 so I cannot be certain of the following but in VB Express (which is what I use) your original code would have tried to do an implicit conversion from the string contained in the textbox to the decimal type defined for dOrderTotal. It would be able to do this provided the string was in a format which was recognised as a number. From your response it appears that in your locale you use "," as the decimal separator so trying to use "." instead would cause the conversion to fail because that would not be recognised as a number.

Doing an explicit conversion, e.g. Convert.ToDecimal would also fail for the same reason. It would make no difference which conversion you tried to do - double, single, decimal - they would all fail because the string being converted is in the wrong format. I cannot explain why ahmedilyas thought it would make a difference.

If the book you are using is of American origin your easiest option might be to go into Control Panel, Regional and Language Options and change to English (United States) whenever you wish to use code from the book.

Dave299 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8
indeed you are right Dave - no idea what I was thinking at the time! My apologies. sometimes the answer is just right there but miss it. Good job
ahmedilyas at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 9

Actually you can use Globalization Culture Info Number format info on the fly instead of having to go through control panel:

You will need to use system.globalization.CultureInfo to setup and use the required numeric format....consider the following as an example:

Dim nfi As Globalization.NumberFormatInfo = New Globalization.CultureInfo("en-US", False).NumberFormat

nfi.NumberGroupSeparator = " "

nfi.NumberDecimalSeparator = ","

Me.TextBox1.Text = "6,5"

Dim sngMyNumber As Single = Convert.ToSingle(Me.TextBox1.Text, nfi)

Dim myInt As Single = 6.5

Console.WriteLine(myInt.ToString("N", nfi))

Console.WriteLine(sngMyNumber.ToString("N", nfi))

DMan1 at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 10

Thanks a lot everybody. I post my question in few different forums, but only here I got really useful answers.

Ovod at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...