|
Hello, I got the error when I was trying to do validation for the part of my program where the user is asked to input the left value for the picture of the man to move to. I've got the movement of the man sorted out but I'm trying to do validation so that the user enters a value that doesn't make the picture go off screen.
This part of the code is where if the user has entered an out of range value. On the bottom loop i get the error "Conversion from string "" to type 'Double' is not valid." Does anyone know how to fix this?
If txtLeft.Text < 10 Then btnRelease.Enabled = False MsgBox("The value you entered for the left was too low", MsgBoxStyle.Exclamation, "Left Input Value Error") txtLeft.Text = ""
End If
If txtLeft.Text > 1240 Then
btnRelease.Enabled = False MsgBox("The value you entered for the left was too high", MsgBoxStyle.Exclamation, "Left Input Value Error") MsgBox("Left:High") txtLeft.Text = "" | | Tom123315 Monday, February 23, 2009 5:29 PM | First off, for that code to even compile, then you must be working with Option Strict Off - a practice I don't suggest you get into the habit of. Option Strict Off should really be reserved for cases where you are working with late bound objects (such as com automation). As for your error - well, it tells you exactly what the problem is. If the txtLeft.Text is empty - has a value of "" then you are going to get an exception. You need to check for that condition in your test: | IftxtLeft.Text.Length<>0AndAlsotxtLeft.Text>1240Then |
Of course, if I were you I would turn on Option Strict and use the Double.TryParse method or one of the built in VB methods to convert the value to a number and then test it.... HTH
Tom Shelton - Marked As Answer byYichun Feng Monday, March 02, 2009 1:34 AM
-
| | Tom Shelton Monday, February 23, 2009 6:10 PM | Hi Tom123315, Welcome to the Vb.Net forums. :-) What Tom Shelton suggests is exactly what I would do too. Use this kind of code within your code as TextBox values are STRINGS not numbers although they can look like a number. So in actual fact if the TextBox has 1234 in it it is a STRING as in | DimaStringAsString="1234" |
then it is still a STRING and always will be. So to see if the text will convert to a number use TryParse like this.>> | DimmyNumberAsDouble=0 | | | Double.TryParse(txtLeft.Text,myNumber) | | | 'Ifthetextis"likeanumber"thenmyNumberwillbethevalueseeninTextBox1 | | 'otherwiseiftheTextintheTextBoxisTOMthenthevaluethenheldbymyNumber=0 |
Then do your IF THEN test on the variable myNumberRegards, John P.S. I have used txtLeft.Text as you have in your code too. ;-) - Edited byJohn Anthony Oliver Monday, February 23, 2009 6:58 PM....
- Edited byJohn Anthony Oliver Monday, February 23, 2009 7:00 PM....
- Marked As Answer byYichun Feng Monday, March 02, 2009 1:34 AM
-
| | John Anthony Oliver Monday, February 23, 2009 6:55 PM | First off, for that code to even compile, then you must be working with Option Strict Off - a practice I don't suggest you get into the habit of. Option Strict Off should really be reserved for cases where you are working with late bound objects (such as com automation). As for your error - well, it tells you exactly what the problem is. If the txtLeft.Text is empty - has a value of "" then you are going to get an exception. You need to check for that condition in your test: | IftxtLeft.Text.Length<>0AndAlsotxtLeft.Text>1240Then |
Of course, if I were you I would turn on Option Strict and use the Double.TryParse method or one of the built in VB methods to convert the value to a number and then test it.... HTH
Tom Shelton - Marked As Answer byYichun Feng Monday, March 02, 2009 1:34 AM
-
| | Tom Shelton Monday, February 23, 2009 6:10 PM | Hi Tom123315, Welcome to the Vb.Net forums. :-) What Tom Shelton suggests is exactly what I would do too. Use this kind of code within your code as TextBox values are STRINGS not numbers although they can look like a number. So in actual fact if the TextBox has 1234 in it it is a STRING as in | DimaStringAsString="1234" |
then it is still a STRING and always will be. So to see if the text will convert to a number use TryParse like this.>> | DimmyNumberAsDouble=0 | | | Double.TryParse(txtLeft.Text,myNumber) | | | 'Ifthetextis"likeanumber"thenmyNumberwillbethevalueseeninTextBox1 | | 'otherwiseiftheTextintheTextBoxisTOMthenthevaluethenheldbymyNumber=0 |
Then do your IF THEN test on the variable myNumberRegards, John P.S. I have used txtLeft.Text as you have in your code too. ;-) - Edited byJohn Anthony Oliver Monday, February 23, 2009 6:58 PM....
- Edited byJohn Anthony Oliver Monday, February 23, 2009 7:00 PM....
- Marked As Answer byYichun Feng Monday, March 02, 2009 1:34 AM
-
| | John Anthony Oliver Monday, February 23, 2009 6:55 PM | Thank you for your replies, they helped a lot | | Tom123315 Friday, March 27, 2009 4:03 PM |
| DimmyNumberAsDouble=0 |
|
| Double.TryParse(txtLeft.Text,myNumber) |
|
| 'Ifthetextis"likeanumber"thenmyNumberwillbethevalueseeninTextBox1 |
| 'otherwiseiftheTextintheTextBoxisTOMthenthevaluethenheldbymyNumber=0 | ...Maybe splitting hairs here but I think the appropriate way to use Try Parse would be to use the return value of the function, which is a boolean value. This is important because if the value in the textbox was "0" and this was a valid input then looking to see if myNumber = 0 would be an erroneous approach. So I'd suggest the following instead...
|
| DimmyNumberAsDouble=0 |
| |
| IfDouble.TryParse(txtLeft.Text,myNumber)Then |
| 'DosomethingwiththisvalidnumerictextvalueusingthevariablemyNumber... |
| Else |
| 'Handleaninvalidinputvalue... |
| EndIf |
|
| That said, of course Tom and John are correct in their views. - Proposed As Answer bydbasnett Friday, March 27, 2009 11:50 PM
-
| | Dig-Boy Friday, March 27, 2009 11:30 PM | Good catch Dig-Boy.
Looking for work - Zip 65101
http://www.vbforums.com/showthread.php?t=552774 | | dbasnett Friday, March 27, 2009 11:50 PM |
|