.toString() Ctype(), cstr(), DirectCast() ?? Which to use?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Ramey

    .toString() Ctype(), cstr(), DirectCast() ?? Which to use?

    Hello,

    There are quite a few ways to convert one object, say an integer to a
    string.

    Dim myStr as string
    dim myInt as integer = 123

    myStr = cstr(myInt)
    myStr = myInt.toString( )
    myStr = CType(myInt, String)
    or use DirectCast()

    What is the preferred, fastest way? Any way I shouldn't be using because it
    could be deprecated?

    Thanks,
    --Michael


  • Trev Hunter

    #2
    Re: .toString() Ctype(), cstr(), DirectCast() ?? Which to use?

    DirectCast won't work for this example - it only works if the run time types
    are the same e.g.

    ------------

    Dim o as Object = "Hello"
    Dim i as Object = Cint(5)
    Dim s as string = directcast(o, String) ' Will work, because o is a string
    at runtime
    Dim s1 as String = directcast(1, String) ' Won't work

    ------------

    As for the other ways, they are more or less the same. In the examples you
    gave, if you follow the IL generated, they you'll notice that all end up
    calling the Int32.ToString( ) method.

    See the other posts in this group relating to this issue:

    "CType() versus Convert.ToXXXX( )"
    "DirectCast vc CType"

    HTH,

    Trev.



    "Michael Ramey" <raterus@localh ost> wrote in message
    news:eVIyOCD7DH A.2952@TK2MSFTN GP09.phx.gbl...[color=blue]
    > Hello,
    >
    > There are quite a few ways to convert one object, say an integer to a
    > string.
    >
    > Dim myStr as string
    > dim myInt as integer = 123
    >
    > myStr = cstr(myInt)
    > myStr = myInt.toString( )
    > myStr = CType(myInt, String)
    > or use DirectCast()
    >
    > What is the preferred, fastest way? Any way I shouldn't be using because[/color]
    it[color=blue]
    > could be deprecated?
    >
    > Thanks,
    > --Michael
    >
    >[/color]


    Comment

    • AirPete

      #3
      Re: .toString() Ctype(), cstr(), DirectCast() ?? Which to use?

      Michael Ramey wrote:[color=blue]
      > Hello,
      >
      > There are quite a few ways to convert one object, say an integer to a
      > string.
      >
      > Dim myStr as string
      > dim myInt as integer = 123
      >
      > myStr = cstr(myInt)
      > myStr = myInt.toString( )
      > myStr = CType(myInt, String)
      > or use DirectCast()
      >
      > What is the preferred, fastest way? Any way I shouldn't be using
      > because it could be deprecated?
      >
      > Thanks,
      > --Michael[/color]

      They are are good ways of doing it, but I prefer ToString simply because I
      feel it's the most readable.
      Pick any of them you like best, and stick with it (for consistancy).

      - Pete


      Comment

      • AirPete

        #4
        Re: .toString() Ctype(), cstr(), DirectCast() ?? Which to use?

        AirPete wrote:[color=blue]
        > Michael Ramey wrote:[color=green]
        >> Hello,
        >>
        >> There are quite a few ways to convert one object, say an integer to a
        >> string.
        >>
        >> Dim myStr as string
        >> dim myInt as integer = 123
        >>
        >> myStr = cstr(myInt)
        >> myStr = myInt.toString( )
        >> myStr = CType(myInt, String)
        >> or use DirectCast()
        >>
        >> What is the preferred, fastest way? Any way I shouldn't be using
        >> because it could be deprecated?
        >>
        >> Thanks,
        >> --Michael[/color]
        >
        > They are are good ways of doing it, but I prefer ToString simply
        > because I feel it's the most readable.
        > Pick any of them you like best, and stick with it (for consistancy).
        >
        > - Pete[/color]

        Oops, I didn't see DirectCast, and as Trev noted, it only works if the types
        are the same.

        - pete


        Comment

        • Trev Hunter

          #5
          Re: .toString() Ctype(), cstr(), DirectCast() ?? Which to use?

          > Oops, I didn't see DirectCast, and as[color=blue]
          > Trev noted, it only works if the types are the same.[/color]

          Or if the types are related by inheritance or interfaces - i.e. a cast is
          avaialble - not a conversion.

          Trev.


          Comment

          • Cor

            #6
            Re: .toString() Ctype(), cstr(), DirectCast() ?? Which to use?

            Hi Michael.

            For .toString you have only to push on the dot.

            Why take another one?

            Do not forget that in every program language a lot of things are for
            backward compatibility.

            Cor


            Comment

            Working...