Make ToString automatic for a class

chopswil

New member
Joined
Jan 2, 2008
Messages
3
Programming Experience
5-10
If you do this code it works fine:
Dim oTest As Date
Dim sTemp As String
oTest = "1/1/2000"
sTemp = oTest

I want to have the same thing for my own class without having to use ToSting:
//assume ToString is overloaded as to Return "stuff"
Dim oClass as myClass
Dim sTemp As String
sTemp=oClass

it would have sTemp have the value of "stuff"

as of now it will give a error that it can't be converted to string.
It can work for the Date data type, so what did MS do for it to make it work?
Ideas?
 
related question
if I have this Code
Private Sub DateTest(ByVal oDate As Date)
'do some stuff
End Sub

and I can call this like so: DateTest("1/1/2008")
VB doesn't complain that I've passed a string to a Date data type.

How does the code work to make this happen?

If I have a class with
Public Sub New(ByVal Value As String)
'do some stuff
End Sub

How can I make something like this work without it complaining about String can't be converted to the Class?
Private Sub ClassTest(ByVal oClass As MyClass)
'do some stuff
End Sub

ClassTest("stuff")
 
That original code will only work if you have Option Strict turned Off, which you should not. Every self-respecting developer should turn Option Strict On, in which case that code would fail twice. The third line is implicitly converting a String to a Date and the fourth line is implicitly converting a Date to a String.

Relying on implicit conversions may seem advantageous because you don't have to think so much about the types of the values you use. As a result though, people tend to not think about things that they should. With Option Strict turned Off your code will be slower to execute and more error-prone.

Option Strict being Off is the only reason that the code in your second post compiles too. With Option Strict On you'd be told that the String couldn't be implicitly converted to a Date.

I cannot suggest strongly enough that you turn Option Strict on, leave it on and perform all your conversions explicitly. You will become a better developer faster as a result. I would never even consider hiring someone who worked with Option Strict Off.

Having said all that, if you still want to be able to implicitly convert your type to String then you have to implement the CType operator:
VB.NET:
Public Class SomeClass

    Public Shared Narrowing Operator CType(ByVal value As SomeClass) As String
        Return value.ToString()
    End Operator

    Public Overloads Overrides Function ToString() As String
        Return "Hello World"
    End Function

End Class
When an implicit conversion is made the types CType operator is invoked. If the CType operator is not implemented for the type being converted to then the conversion fails. The DateTime type has a CType operator defined for the String type so implicit conversions from DateTime to String are possible. If you do as I did above then implicit conversion from your type to String will also be possible. If you want to be able to implicitly convert to other types too then you will need to overload the CType operator and change the return type.

As for your last question, it's just not possible. You can implement your own class such that it possesses the functionality to convert an instance of itself to a String. You cannot change the implementation of the String class though, so you can't make able to convert itself to your type.
 
Last edited:
Note that I originally posted the code above with the CType operator declared as Widening. I then changed it to Narrowing. Thinking about it some more, Widening would probably be the more appropriate. If the type being converted to is capable of holding all possible values of the type being converted from then it should be widening. If there would never be a value of your type that couldn't be converted to a String then it should be Widening. Widening implicit conversions are legal even with Option Strict On. You can read more about the Widening and Narrowing key words in the MSDN Library.
 
so I've turned Option Strict on
how do I get it to re-look at my code?
because it isn't showing the currently written code as errors
 
Back
Top