Home All Groups Group Topic Archive Search About

Difference between Cint and Convert.ToInt32 ?

Author
21 Jul 2006 9:14 PM
Rich
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

Author
21 Jul 2006 10:23 PM
Branco Medeiros
Rich wrote:
<snip>
> 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
Author
21 Jul 2006 11:34 PM
Rich
Thanks.  I guess I will stay with Convert.ToInt32.

Rich


Show quoteHide quote
"Branco Medeiros" wrote:

>
> Rich wrote:
> <snip>
> > 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
>
>
Author
22 Jul 2006 12:22 AM
Herfried K. Wagner [MVP]
"Rich" <R***@discussions.microsoft.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
22 Jul 2006 4:50 AM
Cor Ligthert [MVP]
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

Show quoteHide quote
"Rich" <R***@discussions.microsoft.com> schreef in bericht
news:8BE53B5A-DA04-4844-B911-DC42E14797C0@microsoft.com...
> 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