difference between Convert.ToString(obj) and CType(obj, String)

Discussion in 'ASP .Net' started by Mark Kamoski, Jul 23, 2003.

  1. Mark Kamoski

    Mark Kamoski Guest

    Hi--

    What is the difference between Convert.ToString(obj) and CType(obj,
    String)?

    (Assume obj is a variable of type Object.)

    Please advise.

    Thank you.

    --Mark
    Mark Kamoski, Jul 23, 2003
    #1
    1. Advertising

  2. Mark,
    If obj is type Object, why not just call obj.ToString()?

    Convert.ToString is a framework function. Passing it an Object, causes it to
    use the IConvertable interface that the object implements, String implements
    this interface. I suspect it ultimately calls obj.ToString()

    CType is a VB.NET keyword. It calls some internal VB.NET runtime functions.
    I suspect it ultimately calls obj.ToString()

    Don't forget there is also CStr, which is effectively shorthand for
    CType(obj, String)

    Also, for reference types (String is a reference type). if you know obj is a
    String, DirectCast is better.

    The following articles may help:

    Visual Basic .NET Internals
    http://msdn.microsoft.com/library/d...tml/vbtchmicrosoftvisualbasicnetinternals.asp

    Performance Optimization in Visual Basic .NET
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchPerfOpt.asp

    The first article has a section titled: Conversion Functions, CType,
    DirectCast, and System.Convert that should address your question.

    Hope this helps
    Jay


    "Mark Kamoski" <> wrote in message
    news:...
    >
    > Hi--
    >
    > What is the difference between Convert.ToString(obj) and CType(obj,
    > String)?
    >
    > (Assume obj is a variable of type Object.)
    >
    > Please advise.
    >
    > Thank you.
    >
    > --Mark
    >
    >
    Jay B. Harlow [MVP - Outlook], Jul 23, 2003
    #2
    1. Advertising

  3. Mark Kamoski

    Mark Kamoski Guest

    Jay--

    Which do YOU prefer?

    Which do you recommend?

    Please advise.

    Thank you.

    --Mark


    "Jay B. Harlow [MVP - Outlook]" <> wrote in message
    news:...
    Mark,
    If obj is type Object, why not just call obj.ToString()?

    Convert.ToString is a framework function. Passing it an Object, causes it
    to
    use the IConvertable interface that the object implements, String
    implements
    this interface. I suspect it ultimately calls obj.ToString()

    CType is a VB.NET keyword. It calls some internal VB.NET runtime functions.
    I suspect it ultimately calls obj.ToString()

    Don't forget there is also CStr, which is effectively shorthand for
    CType(obj, String)

    Also, for reference types (String is a reference type). if you know obj is
    a
    String, DirectCast is better.

    The following articles may help:

    Visual Basic .NET Internals
    http://msdn.microsoft.com/library/d...tml/vbtchmicrosoftvisualbasicnetinternals.asp

    Performance Optimization in Visual Basic .NET
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchPerfOpt.asp

    The first article has a section titled: Conversion Functions, CType,
    DirectCast, and System.Convert that should address your question.

    Hope this helps
    Jay


    "Mark Kamoski" <> wrote in message
    news:...
    >
    > Hi--
    >
    > What is the difference between Convert.ToString(obj) and CType(obj,
    > String)?
    >
    > (Assume obj is a variable of type Object.)
    >
    > Please advise.
    >
    > Thank you.
    >
    > --Mark
    >
    >
    Mark Kamoski, Aug 7, 2003
    #3
  4. Mark,
    I prefer to use the one that is appropriate at the time!

    I recommend using the one that is appropriate at the time!

    If I were a wood worker: When I needed a pocket knife, I would use a pocket
    knife. When I needed a hatchet, I would use a hatchet. When I needed a chain
    saw, I would use a chain saw.

    In other words, there is no simple answer. There is no magic bullet. You
    need to read the articles, learn, formulate ideas.

    If you look at my code, when I have any variable and I need a string, I
    normally use ToString (sometimes I use String.Format, sometimes other
    functions). When I have an object, and I need a specific type, I use
    DirectCast. When DirectCast is not appropriate I use CType (enums mostly).
    However these are not hard and fast rules, these are applying what you learn
    by reading the articles we gave you applying the knowledge contained within.

    Hope this helps
    Jay

    "Mark Kamoski" <> wrote in message
    news:...
    >
    >
    > Jay--
    >
    > Which do YOU prefer?
    >
    > Which do you recommend?
    >
    > Please advise.
    >
    > Thank you.
    >
    > --Mark
    >
    >
    > "Jay B. Harlow [MVP - Outlook]" <> wrote in

    message
    > news:...
    > Mark,
    > If obj is type Object, why not just call obj.ToString()?
    >
    > Convert.ToString is a framework function. Passing it an Object, causes it
    > to
    > use the IConvertable interface that the object implements, String
    > implements
    > this interface. I suspect it ultimately calls obj.ToString()
    >
    > CType is a VB.NET keyword. It calls some internal VB.NET runtime

    functions.
    > I suspect it ultimately calls obj.ToString()
    >
    > Don't forget there is also CStr, which is effectively shorthand for
    > CType(obj, String)
    >
    > Also, for reference types (String is a reference type). if you know obj is
    > a
    > String, DirectCast is better.
    >
    > The following articles may help:
    >
    > Visual Basic .NET Internals
    >

    http://msdn.microsoft.com/library/d...tml/vbtchmicrosoftvisualbasicnetinternals.asp
    >
    > Performance Optimization in Visual Basic .NET
    >

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchPerfOpt.asp
    >
    > The first article has a section titled: Conversion Functions, CType,
    > DirectCast, and System.Convert that should address your question.
    >
    > Hope this helps
    > Jay
    >
    >
    > "Mark Kamoski" <> wrote in message
    > news:...
    > >
    > > Hi--
    > >
    > > What is the difference between Convert.ToString(obj) and CType(obj,
    > > String)?
    > >
    > > (Assume obj is a variable of type Object.)
    > >
    > > Please advise.
    > >
    > > Thank you.
    > >
    > > --Mark
    > >
    > >

    >
    >
    >
    Jay B. Harlow [MVP - Outlook], Aug 9, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,050
  2. Matthew Thorley

    How do you convert a string obj to a file obj?

    Matthew Thorley, May 4, 2005, in forum: Python
    Replies:
    7
    Views:
    488
    Peter Otten
    May 4, 2005
  3. narsing
    Replies:
    2
    Views:
    870
    Thomas Weidenfeller
    Oct 23, 2006
  4. Fergus McMenemie

    LibXML element->toString vs document->toString

    Fergus McMenemie, Jul 12, 2012, in forum: Perl Misc
    Replies:
    5
    Views:
    606
    Fergus McMenemie
    Jul 17, 2012
  5. Johannes Schneider
    Replies:
    0
    Views:
    54
    Johannes Schneider
    Dec 11, 2013
Loading...

Share This Page