The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Convert.ToString vs ToString()

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eric Wise

Posts: 126
Nickname: ewise
Registered: Apr, 2005

Eric Wise is a senior .NET consultant.
Convert.ToString vs ToString() Posted: Apr 13, 2005 12:04 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Eric Wise.
Original Post: Convert.ToString vs ToString()
Feed Title: Eric Wise
Feed URL: /error.htm?aspxerrorpath=/blogs/eric.wise/rss.aspx
Feed Description: Business & .NET
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Eric Wise
Latest Posts From Eric Wise

Advertisement

What's the difference? Here's a console application that shows one:

        Dim myObject As Object = Nothing
        Dim toStringTest As String
        Dim convertTest As String

        Try
            toStringTest = myObject.ToString()
        Catch ex As Exception
            Console.WriteLine("MyObject.ToString Error: " & ex.Message)
        End Try

        Try
            convertTest = Convert.ToString(myObject)
        Catch ex As Exception
            Console.WriteLine("Convert.ToString(MyObject) Error: " & ex.Message)
        End Try

        If TypeOf toStringTest Is String Then
            Console.WriteLine("toStringTest is a string!")
        Else
            Console.WriteLine("toStringTest is NOT a string!")
        End If

        Try
            If TypeOf (toStringTest.ToString()) Is String Then
                Console.WriteLine("toStringTest.ToString() is a string!")
            Else
                Console.WriteLine("toStringTest.ToString() is NOT a string!")
            End If
        Catch ex As Exception
            Console.WriteLine("toStringtext.ToString() Error: " & ex.Message)
        End Try

        Try
            If TypeOf convertTest Is String Then
                Console.WriteLine("convertTest is a string!")
            Else
                Console.WriteLine("convertTest is NOT a string!")
            End If
        Catch ex As Exception
            Console.WriteLine("convertTest.ToString() Error: " & ex.Message)
        End Try

Output:

MyObject.ToString Error: Object reference not set to an instance of an object.


toStringTest is NOT a string!


toStringtest.ToString() Error: Object reference not set to an instance of an object.


convertTest is a string!

Read: Convert.ToString vs ToString()

Topic: Easy Assets .NET : Demo Previous Topic   Next Topic Topic: Code worst

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use