Question Error: InvalidCastException was unhandled

JSVBnet

New member
Joined
Jun 28, 2012
Messages
2
Programming Experience
Beginner
Hi everyone,

I'm new to this forum and I just started learning vb.net this summer for my class.

I ran into a conversion problem when I was trying to run my program. An error popped up saying that "conversion from string "select one" to type "double" is not valid."

It was weird because I used cdbl() and this had worked before at my other program. I don't know why it's not working this time. =(

Anyone has any ideas?



Thanks alot!
 
Why would it be weird that CDbl wouldn't be able to cast the String "select one" as type Double? Exactly what Double value would you expect that to produce?

My guess is that you have a ComboBox that contains numbers and you have a default prompt and you are assuming that the user will select one of the numbers but in this case they haven't. My first question is: why do you need the prompt at all? Do you have a prompt in your TextBoxes that says "type something"? Of course not, because you know that your users are not moronic enough to need to be told to type something into a TextBox. Why then do you think that they are moronic enough to need to be told to select an item in a ComboBox?

Just like your TextBoxes, your ComboBox should be empty by default, with no item selected. You can then first test whether a selection has been made and ONLY try to convert/cast IF there has been.
 
Why would it be weird that CDbl wouldn't be able to cast the String "select one" as type Double? Exactly what Double value would you expect that to produce?

My guess is that you have a ComboBox that contains numbers and you have a default prompt and you are assuming that the user will select one of the numbers but in this case they haven't. My first question is: why do you need the prompt at all? Do you have a prompt in your TextBoxes that says "type something"? Of course not, because you know that your users are not moronic enough to need to be told to type something into a TextBox. Why then do you think that they are moronic enough to need to be told to select an item in a ComboBox?

Just like your TextBoxes, your ComboBox should be empty by default, with no item selected. You can then first test whether a selection has been made and ONLY try to convert/cast IF there has been.


It's working now! The error showed up because I had

dim startnum as double = cdbl(tbxstartnum.text)

but my textbox was names something other than tbxstartnum so the program couldn't find a matching textbox to input the information.


Thanks anyway! =)
 
Back
Top