-
Mar 16th, 2011, 08:00 PM
#1
Thread Starter
New Member
-
Mar 16th, 2011, 08:10 PM
#2
Addicted Member
Re: HELP! Conversion from string "" to type 'Double' is not valid.
To break this down to layman's terms....
You're asking it to convert "" to a number. I'm not sure what number you think it should pick....
If you convert "3" to a number its 3
I'm not sure how it could convert "" to a number....
While you test to see if txtInitial.Text <> "" you only show a messagebox, and then try to perform the conversion, perhaps you need to move the conversation/math code to a different place?
While the following is not your solution it is something to know for the future....
sometimes in the middle of a sub you need to exit the sub and you use "Exit Sub", but you need to move your mathmatical code somewhere inside your "test" to see if txtInitial.Tex <> "", I'm sure you can figure this out....use basic logic.
-
Mar 16th, 2011, 08:11 PM
#3
Re: HELP! Conversion from string "" to type 'Double' is not valid.
The error message is fairly self-explanatory: you can't convert an empty string to a number. This is an example of why a real-world application would very rarely use CDbl to convert the contents of a TextBox to a number. You're obviously clicking your Button without entering anything into your TextBox, which is just the sort of thing that real users do. It's for that reason that validation is essential in real-world apps.
In a learning context though, you are often told things like "you can assume that all input is valid" so that you can concentrate on processing the data without having to worry about validation. If that's so in your case then you must make sure that you always enter valid data when testing.
If you can't make that assumption then you must validate first. You already know that an empty string is invalid, but even a non-empty string doesn't guarantee a valid number. It's for that reason that most real-world apps will use a TryParse method, e.g. Double.TryParse, to both validate and convert in one pass.
-
Mar 16th, 2011, 08:15 PM
#4
Addicted Member
Re: HELP! Conversion from string "" to type 'Double' is not valid.
Also, it will probably serve you well to use...
Messagebox.show("This is the .net way to show a message box")
verse
MsgBox("This is the VB6 way to show a message box")
-
Mar 16th, 2011, 08:18 PM
#5
Addicted Member
Re: HELP! Conversion from string "" to type 'Double' is not valid.
jmcilhinney makes a great point (as usual)
How do you convert "Hello World" to a number, simply isnt going to work, and it wont get caught in your "test" if then...
-
Mar 16th, 2011, 08:38 PM
#6
Thread Starter
New Member
Re: HELP! Conversion from string "" to type 'Double' is not valid.
thanks for the quick reply everyone
i just couldn't figure out why it wasn't working after entering the 'msgbox' but now i've figured out what to do.
this forum is going to be very helpful
thanks
-
Mar 16th, 2011, 08:43 PM
#7
Re: [RESOLVED] HELP! Conversion from string "" to type 'Double' is not valid.
I just had a closer look at your code and this part is dodgy for several reasons:
vb.net Code:
If txtInitial.Text <> "" Or txtLengthOfLoan.Text <> "" Or txtInterestRate.Text <> "" Then Else MsgBox("No data entered, please enter data") txtInitial.Focus() End If
First of all, you should NEVER have an empty If block. If you do, you should invert the condition, move the code from the Else block to the If block and get rid of the Else block.
Second, you should almost always use AndAlso and OrElse in preference to And and Or.
Third, as suggested by 7777, you don't exit out when you find an invalid state, so the subsequent code will be executed no matter what.
Finally, your conditional code isn't doing what it should. It should be continuing only if all three fields are not empty, which is equivalent to not continuing if any field is empty. Your code doesn't test for either of those fields.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|