decimal.parse

otuatail

Active member
Joined
May 25, 2011
Messages
28
Programming Experience
5-10
Hi I get this error "Input string was not in a correct format." when getting information from a text box. I have the following

Dim E_Daily As Decimal
E_Daily = Decimal.Parse(SP_E_Dy.ToString()) ' Text box value = 15.14


This should be ok, shouldent it?

TIA

Desmond.
 
Use TryParse

Do you have Option Strict On at the top of your code? If not, make sure it is on.

Use the TryParse() method instead of Parse. It has 2 arguments -- the first is the string to convert, and the second is the number variable to convert it into, using the same type as indicated in the TryParse command. Any numeric type is supported. The value in the textbox is a string, no matter whether or not it's numeric. Use code similar to the following:

VB.NET:
Dim decnum As Decimal
Decimal.TryParse(Textbox1.Text, decnum)
MessageBox.Show(decnum.ToString)

If the value is not numeric, or empty, TryParse will return 0 without an error.
 
No this is defiantly not right

? SP_E_Dy.ToString()
"System.Windows.Forms.TextBox, Text: 15.14"

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim E_Units As Decimal
Dim G_Units As Decimal
E_Units = Decimal.Parse(E_Units.ToString()) ' This is Zero should be 3838
G_Units = Decimal.Parse(G_Units.ToString()) ' This is Zero should be 8452
'Scottish power ***
Dim E_Daily As Decimal
Dim G_Daily As Decimal
E_Daily = Decimal.Parse(SP_E_Dy.ToString())
G_Daily = Decimal.Parse(SP_G_Dy.ToString())
Dim E_Total As Decimal
E_Total = (E_Daily * 92 / 100)
End Sub

Moving my mouse over the code at a break point does not show any tool tip with value.
And this

? E_Units.text
'text' is not a member of 'Decimal'.

This is crazy.
 
Hi I get this error "Input string was not in a correct format." when getting information from a text box. I have the following

Dim E_Daily As Decimal
E_Daily = Decimal.Parse(SP_E_Dy.ToString()) ' Text box value = 15.14


This should be ok, shouldent it?

TIA

Desmond.
This code is converting the entire TextBox class instance into a string, which is much more than just it's Text property (which holds the 15.14 you're looking for). What you need is to pass the Text property (which is already a string so no need to .ToString() the Text property) to the Decimal.Parse():
VB.NET:
Dim E_Daily As Decimal
E_Daily = Decimal.Parse(SP_E_Dy.Text) ' Text box value = 15.14
However, given that this is input coming from a TextBox, which means the user can type whatever they want to into it, you'll want to test the input before actually parsing it into a Decimal, of which the Decimal class has a TryParse method you can use for just that:
VB.NET:
Dim E_Daily As Decimal
If Not Decimal.TryParse(SP_E_Dy.Text, E_Daily) Then
    'It failed, notify user or something
End If
However, if they don't put anything in the TextBox then even that will fail because the TryParse expects a non-empty String, so you should sanitize that a little bit too:
VB.NET:
Dim E_Daily As Decimal

Dim TheText As String = If(SP_E_Dy.Text.Trim.Length > 0I, SP_E_Dy.Text.Trim, "0") 'Set it equal to a parse-able zero

If Not Decimal.TryParse(TheText, E_Daily) Then
    'It failed, notify user or something
End If
But then again we should stop and think about this for a minute, you're wanting to get a number from the user & you're using a completely unrestricted TextBox control and you're hoping they'll always input text that can actually be converted to a numeric value, shouldn't we be using a control that's actually meant to grab numbers from the user? maybe, say the NumericUpDown control? With that all you'd need is to set the minimum & maximum values (the range) & of course the increment values (note the user can use the keyboard to type the number they want to into it, or even paste a number from the clipboard into it too): http://www.vbdotnetforums.com/windows-forms/28288-numericupdown.html
 
However, if they don't put anything in the TextBox then even that will fail because the TryParse expects a non-empty String, so you should sanitize that a little bit too:

No, it will NOT fail with TryParse(). If the value is either empty or non-numeric, it will return 0 without an error. The following will work no matter what:

If Not Decimal.TryParse(TheText, E_Daily) Then
'It failed, notify user or something
End If
 
No, it will NOT fail with TryParse(). If the value is either empty or non-numeric, it will return 0 without an error. The following will work no matter what:
I see that now, I see they've also updated the documentation, I'm not sure when they fixed that as I remember it throwing a NullReference exception if the String param is null/nothing & an InvalidParameter exception if you passed it an empty String (String.Empty) back in VS2005. I'm glad to see that it's no longer the case because I was confused why they did it that way back then.
 
Back
Top