Results 1 to 4 of 4

Thread: [Resolved] Difference between convert.tostring and Ctype

  1. #1

    Thread Starter
    Lively Member Xcoder's Avatar
    Join Date
    Jan 2004
    Posts
    120

    [Resolved] Difference between convert.tostring and Ctype

    Is there any?
    Last edited by Xcoder; Oct 9th, 2006 at 08:55 AM.
    Last edited by Xcoder : 09-10-2001 at 12:45 AM.

  2. #2
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Difference between convert.tostring and Ctype

    Convert - Converts a base data type to another base data type
    http://msdn2.microsoft.com/en-us/lib...m.convert.aspx

    CType - Returns the result of explicitly converting an expression to a specified data type, object, structure, class, or interface.
    http://msdn2.microsoft.com/en-us/library/4x2877xb.aspx
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Difference between convert.tostring and Ctype

    ToString should used when you want to obtain a string representation of an object that is not a string, e.g. displaying a number in a TextBox:
    VB Code:
    1. Dim myNumber As Integer = 100
    2.  
    3. myTextBox.Text = myNumber.ToString()
    CType, or the specialised versions CStr, CInt, CDbl, etc. should be used when you need to cast a reference to the specific type of the object. What I mean by that is that you should only use CType when the object itself is already that type, but it is being accessed via a reference of a different type, e.g. getting an item from an ArrayList:
    VB Code:
    1. Dim myCollection As New ArrayList
    2.  
    3. myCollection.Add("Hello World")
    4.  
    5. Dim myString As String = CType(myCollection(0), String)
    In this case the object itself is already a String, but the ArrayList.Item property returns an Object reference, so you need to cast it as type String in order to treat it as a String. You can use CType, CStr, CInt, etc. to perform conversions in some instances but you shouldn't. You should use those operators only for casting. For instance, this is quite valid:
    VB Code:
    1. Dim myString As String = "100"
    2. Dim myNumber As Integer = CType(myString, Integer)
    but you should not do that. If the object itself is being changed from one type to another then you should use proper conversion functions, e.g.
    VB Code:
    1. Dim myString As String = "100"
    2. Dim myNumber As Integer = Convert.ToInt32(myString)
    Also, despite what some people would have you believe there is nothing wrong with using CStr, CInt, CDbl, etc. They are simply specialised versions of CType for each inbuilt VB data type. If you use Integer instead of Int32 then you should use CInt instead of CType(X, Integer) because they are equivalent.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Frenzied Member
    Join Date
    Aug 2006
    Posts
    1,051

    Re: [Resolved] Difference between convert.tostring and Ctype

    What is the internal difference between Convert.To... and Ctype? Thanks Again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width