M
Mike S
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?
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?