472,959 Members | 1,613 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,959 software developers and data experts.

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 1922
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Neil Zanella | last post by:
Hello, I wonder whether anyone has ever come across the following g++ compiler error message. I don't recall ever seeing it before. I solved my problem but I am still not sure about what this...
15
by: buda | last post by:
Let me see if I got this :) 1. I know the rules for type conversions in arithmetic expressions 2. I know that an implicit type conversion is done at assignment, so float x = 1.23; int t = (int)...
5
by: Wilfried Mestdagh | last post by:
Hi, I have a class that holds data in array of byte. It has property to get the data like this (FBuffer is a byte): public byte Data { get { return FBuffer; } }
3
by: Rudy | last post by:
I am writing a program in VB.NET and as I was debugging a problem I noticed my For loop doesn't want to loop! I originally had a upper bound which was an expression and it wasn't working. WHen I...
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
6
by: =?Utf-8?B?RGF2ZQ==?= | last post by:
I found a really strange quirk in the C# compiler, which I can't beleive is proper behaviour. If I define a class thus: public class MyClass { public override string ToString() { return...
31
by: Zytan | last post by:
Everything (er, every class) in C# has ToString() which is conveniently automatically invoked when using it in Debug.WriteLine() or in a string concatenation, etc. I made a struct, and I want to...
15
by: Jason Doucette | last post by:
If you have two overloaded functions: double maximum(double a, double b, double c); int maximum(int a, int b, int c); They work fine if you call maximum() with all arguments as doubles, or...
35
by: =?Utf-8?B?UElFQkFMRA==?= | last post by:
I'd really like to be able to constrain a generic type to System.Enum or, better, enum. But of course that results in "Compiler Error CS0702". So far I've been checking the type parameter at...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.