Difference between Cint and Convert.ToInt32 ?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello

txtID.Text contains a 6 digit Integer number with no decimals, no chars...

Dim j As Integer = Cint(txtID.Text)

or

Dim j As Integer = Convert.ToInt32(txtID.Text)

Is there any difference between these 2 statements? Is one statement more
correct than the other?

Thanks,
Rich
 
Rich wrote:
txtID.Text contains a 6 digit Integer number with no decimals, no chars...

Dim j As Integer = Cint(txtID.Text)

or

Dim j As Integer = Convert.ToInt32(txtID.Text)

Is there any difference between these 2 statements? Is one statement more
correct than the other?
<snip>

Well, there *is* a difference:

any the following literals would raise an exception if Convert.ToInt32
was used instead of CInt:

? CInt("123.456")
? CInt("123,456")
? CInt("&h123")

In other words, CInt (when passed a string) performs more work than
Convert.ToInt32.

Regards,

Branco
 
Rich said:
txtID.Text contains a 6 digit Integer number with no decimals, no chars...

Dim j As Integer = Cint(txtID.Text)

or

Dim j As Integer = Convert.ToInt32(txtID.Text)

Is there any difference between these 2 statements? Is one statement more
correct than the other?

Not really. I'd try to avoid 'Convert.ToInt32' and use 'CInt' or
'Integer.Parse' instead.
 
Rich,

Yes there are,
Convert.ToInt32 is longer and it converts to Int32
CInt converts to Integer what should be represent the best internal format
to use by the computer. Unlucky enough has a big lobby changed the last for
the 64bit version and are there extra instructions needed now (they are fast
but still take time).

Cor
 
Back
Top