a simple question

E

Edward

hi everybody,
There is an error that drives me crazy and I can't figure out why VB.net
behaves this way.
I have a form and a textbox and the following code in the form Loads event
Dim Ab As Integer
Ab = CInt(TextBox1.Text)
but it generates InvalidCastException ! but I know we can caste from string
to integer so what is going on here ? Any thoughts?
 
J

Jack Jackson

hi everybody,
There is an error that drives me crazy and I can't figure out why VB.net
behaves this way.
I have a form and a textbox and the following code in the form Loads event
Dim Ab As Integer
Ab = CInt(TextBox1.Text)
but it generates InvalidCastException ! but I know we can caste from string
to integer so what is going on here ? Any thoughts?

Sure. You can only convert from String to Integer if the string
contains a valid Integer value. Whatever is in TextBox1.Text is not a
valid integer. Maybe something like "abc", or
"1111111111111111111111111111111111111111111111".

Put a breakpoint on that line and see what is in the textbox.
 
E

Edward

In fact there is nothing in the textbox I was just testing this simple code
on a new form and I get InvalidCasteException during the compile process ( It
doesn't compile ).
 
E

Edward

Okay after a few tries I noticed that this code works only I use it in a try
catch block. Is this really the way VB.net works I mean to read data from
atext box we must have it in a try catch block !!!?
 
L

Lloyd Sheen

Edward said:
Okay after a few tries I noticed that this code works only I use it in a
try
catch block. Is this really the way VB.net works I mean to read data from
atext box we must have it in a try catch block !!!?

Try the following:

TryParse this is a shared Integer method that will test the input to the
parse and let you know if the integer was set with a value or if the value
being parsed cannot be parsed. This will ensure that an attempt to parse an
non numeric value will fail without causing problems as long as you test the
return value from TryParse.

LS
 
T

Tom Shelton

Okay after a few tries I noticed that this code works only I use it in a try
catch block. Is this really the way VB.net works I mean to read data from
atext box we must have it in a try catch block !!!?

No... An empty string does not convert to an integer value. So, either
set the default text in the textbox to a valid integer value - or use
Integer.TryParse to avoid an exception...

Dim i As Integer
Integer.TryParse (textBox1.Text, i)

Of course, you can test the result of tryparse if you care about failure
- and it will fail in this case. But, i will be zero :)
 
F

Family Tree Mike

Do you really get the error at compile time? I don't in VB 2008 Express, nor
would I have expected to get the error until runtime.

BTW, an InvalidCasteException should only occur in India... :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top