468,110 Members | 1,524 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,110 developers. It's quick & easy.

Why doesn't the compiler use ToString for implicit conversions?

Maybe I'm missing some fundamental unwritten law of OOP, but I was
wondering why the VB.NET compiler doesn't take advantage of the fact
that all .NET objects, being derived from the Object base class, have
a ToString method defined on them, when a reference type is used in an
expression where a String is expected. I mean if you define a class
MyClass in MyNamespace and don't override ToString, calling ToString
will return the qualified type name: "MyNamespace.MyClass" (the
default behavior). If you then do this:

Dim myObject As MyNamespace.MyClass
Dim str As String

str = "MyClass converted to a string = " & myObject

The compiler will complain that myObject cannot be cast to String. The
compiler obviously knows what types are involved in any potential
conversion, so why can't it just call myObject.ToString to perform the
conversion on realizing that myObject should be converted to String?
In another OOP language this might not make sense, since the compiler
can't assume a method called ToString exists on any given object, but
since .NET objects by definiton must all inherit from the common
Object base class, and ToString is part of Object's defintion, why
shouldn't the compiler just call ToString, since it knows the method
will be there?

Now, obviously, you can easily get around all this by explicitly
calling ToString yourself, but I'm wondering why the compiler won't
simply do it automatically (perhaps it could perform it only as long
if Option Strict is Off to give the programmer a choice).

I mean, the compiler will implicitly convert a value type into a
String when necessary, why not extend this to reference types in the
specific case of converting to String, since, again, the ToString is
readily available?

I guess what I'm getting at is this: is there a good reason why it
doesn't work this way? Especially when you consider that the following
code works without needing to explicitly call myObject.ToString:

Console.WriteLine(myObject)

I assume this works because the parameters to WriteLine are of type
Object, and my guess is that WriteLine simply calls ToString on the
objects passed to it to get a String suitable for output. So, it's
more to do with how WriteLine is implemented rather than a situtation
where a compiler-defined conversion occurs, but what's stopping the
compiler from doing the same thing in the specific case of converting
a reference type to String?

--
Mike

Apr 27 '07 #1
3 1776
I don't know the right reasons for that. But one of them might be the
fact that you can redefine ToString so that it doesn't return string:

Class MyClass
''' <summary>Crazy ToString.</summary>
Public Shadows Function ToString() As Collection
Return New Collection
End Function
End Class

And I think the "Shadows" keyword is not even necessary here.
--
Peter Macej
Helixoft - http://www.helixoft.com
VSdocman - Commenter and generator of class documentation for C#, VB
..NET and ASP .NET code
Apr 27 '07 #2
>I don't know the right reasons for that. But one of them might be the
fact that you can redefine ToString so that it doesn't return string:
But you (and the compiler) can still call the base definition.

C# supports this so its definitely doable.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 27 '07 #3
By requiring the use of the ToString() method in code, the VB 2005 compiler
eliminates an entire class of possible bugs caused by incorrect type
casting. Yes, C# can do this, but IMO doing so leads to harder to read
code.

Mike Ober.
"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:uL**************@TK2MSFTNGP02.phx.gbl...
>
>>I don't know the right reasons for that. But one of them might be the
fact that you can redefine ToString so that it doesn't return string:

But you (and the compiler) can still call the base definition.

C# supports this so its definitely doable.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Apr 28 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Neil Zanella | last post: by
15 posts views Thread by buda | last post: by
5 posts views Thread by Wilfried Mestdagh | last post: by
36 posts views Thread by Chad Z. Hower aka Kudzu | last post: by
6 posts views Thread by =?Utf-8?B?RGF2ZQ==?= | last post: by
35 posts views Thread by =?Utf-8?B?UElFQkFMRA==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.