CInt, CType, or DirectCast? 
Author Message
 CInt, CType, or DirectCast?

If I have a string type that contains a number and I want to convert
it to an integer type, what is the best performing method?

=====================
dim sString as string = "4"
dim iInt as integer

iInt = CInt(sString)
iInt = CType(sString, integer)
iInt = DirectCast(sString, integer)
=====================

Thanks,
John



Tue, 20 Sep 2005 06:33:43 GMT  
 CInt, CType, or DirectCast?
DirectCast

Erik Porter
Microsoft .NET MVP
http://www.windowsforms.net


Quote:
> If I have a string type that contains a number and I want to convert
> it to an integer type, what is the best performing method?

> =====================
> dim sString as string = "4"
> dim iInt as integer

> iInt = CInt(sString)
> iInt = CType(sString, integer)
> iInt = DirectCast(sString, integer)
> =====================

> Thanks,
> John



Tue, 20 Sep 2005 06:37:38 GMT  
 CInt, CType, or DirectCast?
Hello John,


Quote:
> If I have a string type that contains a number and I
> want to convert it to an integer type, what is the best
> performing method?

> =====================
> dim sString as string = "4"
> dim iInt as integer

> iInt = CInt(sString)
> iInt = CType(sString, integer)
> iInt = DirectCast(sString, integer)

or my preferred method:

\\\
iInt = Integer.Parse(sString)
///

=====================

;-)

Regards,
Herfried K. Wagner



Tue, 20 Sep 2005 06:37:22 GMT  
 CInt, CType, or DirectCast?

Quote:
>DirectCast

... doesn't work in this case, since it only does casting and
unboxing, not conversion.

CInt() and CType(,Integer) should always compile to the same code, so
there's no difference there.

Mattias

===

http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.



Tue, 20 Sep 2005 06:52:34 GMT  
 CInt, CType, or DirectCast?

Quote:
> If I have a string type that contains a number and I want to convert
> it to an integer type, what is the best performing method?

> =====================
> dim sString as string = "4"
> dim iInt as integer

> iInt = CInt(sString)
> iInt = CType(sString, integer)
> iInt = DirectCast(sString, integer)
> =====================

Well DirectCast doesn't work, and the other 2 are identical.

Here's the MSIL
.method public static void  Main() cil managed
{
  .entrypoint
  .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01
00 00 00 )
  // Code size       35 (0x23)
  .maxstack  1
  .locals init ([0] int32 iInt,
           [1] string sString)
  IL_0000:  nop
  IL_0001:  ldstr      "4"
  IL_0006:  stloc.1
  IL_0007:  ldloc.1
  IL_0008:  call       int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.IntegerType::F
romString(string)
  IL_000d:  stloc.0
  IL_000e:  ldloc.1
  IL_000f:  call       int32
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.IntegerType::F
romString(string)
  IL_0014:  stloc.0
  IL_0015:  ldloc.1
  IL_0016:  unbox      [mscorlib]System.Int32
  IL_001b:  ldobj      [mscorlib]System.Int32
  IL_0020:  stloc.0
  IL_0021:  nop
  IL_0022:  ret

Quote:
} // end of method Module1::Main

David


Tue, 20 Sep 2005 07:36:56 GMT  
 CInt, CType, or DirectCast?

Quote:
> If I have a string type that contains a number and I want to
> convert it to an integer type, what is the best performing method?

> =====================
> dim sString as string = "4"
> dim iInt as integer

> iInt = CInt(sString)
> iInt = CType(sString, integer)
> iInt = DirectCast(sString, integer)
> =====================

  iInt = Integer.Parse(sString)

;-)

Among those you mentioned: CInt.
CType does the same but is more to type.
DirectCast doesn't convert objects, but it casts the reference and is not
possible here.

--
Armin



Tue, 20 Sep 2005 06:48:58 GMT  
 CInt, CType, or DirectCast?


Quote:

> > If I have a string type that contains a number and I want to
> > convert it to an integer type, what is the best performing method?

> > =====================
> > dim sString as string = "4"
> > dim iInt as integer

> > iInt = CInt(sString)
> > iInt = CType(sString, integer)
> > iInt = DirectCast(sString, integer)
> > =====================

>   iInt = Integer.Parse(sString)

Looks like Integer.Parse is the winner.  Both the VB conversion methods end
up calling Double.Parse.
While Integer.Parse ultimitely calls System.Number.ParseInt32

.method public hidebysig static int32  ParseInt32(string s,
                                                  valuetype
System.Globalization.NumberStyles style,
                                                  class
System.Globalization.NumberFormatInfo info) cil managed internalcall
{

Quote:
} // end of method Number::ParseInt32

The implementation of which is hidden, but it doesn't look like it involves
conversion to a double first.

David



Tue, 20 Sep 2005 09:11:25 GMT  
 CInt, CType, or DirectCast?



Quote:


> > If I have a string type that contains a number and I want to convert
> > it to an integer type, what is the best performing method?

> > =====================
> > dim sString as string = "4"
> > dim iInt as integer

> > iInt = CInt(sString)
> > iInt = CType(sString, integer)
> > iInt = DirectCast(sString, integer)
> > =====================

> Well DirectCast doesn't work, and the other 2 are identical.

> Here's the MSIL
> .method public static void  Main() cil managed
> {
>   .entrypoint
>   .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = (
01
> 00 00 00 )
>   // Code size       35 (0x23)
>   .maxstack  1
>   .locals init ([0] int32 iInt,
>            [1] string sString)
>   IL_0000:  nop
>   IL_0001:  ldstr      "4"
>   IL_0006:  stloc.1
>   IL_0007:  ldloc.1
>   IL_0008:  call       int32

[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.IntegerType::F
Quote:
> romString(string)
>   IL_000d:  stloc.0
>   IL_000e:  ldloc.1
>   IL_000f:  call       int32

[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.IntegerType::F

Quote:
> romString(string)
>   IL_0014:  stloc.0
>   IL_0015:  ldloc.1
>   IL_0016:  unbox      [mscorlib]System.Int32
>   IL_001b:  ldobj      [mscorlib]System.Int32
>   IL_0020:  stloc.0
>   IL_0021:  nop
>   IL_0022:  ret
> } // end of method Module1::Main

> David

What about System.Convert.ToInt32()?

Tom Shelton



Tue, 20 Sep 2005 13:08:08 GMT  
 CInt, CType, or DirectCast?

Quote:

> What about System.Convert.ToInt32()?

System.Convert.ToInt32 calls Integer.Parse.

--
Armin



Tue, 20 Sep 2005 20:36:51 GMT  
 CInt, CType, or DirectCast?


Quote:

> > What about System.Convert.ToInt32()?

> System.Convert.ToInt32 calls Integer.Parse.

> --
> Armin

Gotch ya!
Tom Shelton


Tue, 20 Sep 2005 22:07:35 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. DirectCast, why?

2. Cint or Ctype.. which is recommended?

3. VB5: CInt(4.5) = 4; CInt(4.5000000001) = 5?

4. Rounding with CInt

5. cType() Function

6. VB Type Conversion vs. Ctype

7. cType()

8. CType performances with VS2003!!!

9. CType function issue in Web service proxy

10. --> Error with CType

11. How to CType() dynamically ?

12. Dynamic Ctype question

 

 
Powered by phpBB® Forum Software