I can successfully use CStr()
on boxed built-in vb.net type. But how I can achieve the same with boxed custom type? I'm getting
Exception thrown: 'System.InvalidCastException' in Microsoft.VisualBasic.dll
The codez:
' sample custom type
Structure Record
Public Property Value As Integer
Overloads Function TOSTRING() As String ' capitalizaition intentional to reveal usage
Return ">>" & Value.ToString() & "<<"
End Function
Shared Operator &(left As String, right As Record) As String
Return left & right.TOSTRING()
End Operator
Shared Widening Operator CType(left As Record) As String
Return left.TOSTRING()
End Operator
End Structure
' both use cases
Sub Main()
' demo with built-in type
Dim i As Integer = 3
Dim ib As Object = i ' boxed into Object
Debug.Print("i:" & CStr(i))
Debug.Print("ib:" & CStr(ib)) ' works OK
' demo with custom type
Dim r As New Record With {.Value = 3}
Dim rb As Object = r ' boxed into Object
Debug.Print("r:" & CStr(r))
Debug.Print("rb:" & CStr(rb)) ' Exception thrown:
' 'System.InvalidCastException' in Microsoft.VisualBasic.dll
End Sub