Search code examples
vb.netstructuretostringobject-to-stringdata-representation

Can default strings representing structure printout be changed?


I'm trying to use common technique to create structure mimicking native type, it is constrained number (constraint not shown in sample):

<HideModuleName> _
Public Module DbKeyModule

    <DebuggerDisplay("ID = {Value}")> _
    Structure DbKey
        Implements IComparable(Of DbKey)
        Implements IEquatable(Of DbKey)

        Const Null As Integer = 0

        ReadOnly Property Value() As Integer
            Get
                Return _Value
            End Get
        End Property

        Overloads Function ToString() As String
            Return If(_Value <> 0, _Value.ToString(InvariantCulture.NumberFormat), "NULL")
        End Function
        'GetHashCode(), Equals(), CompareTo() follow
        'constructors follow
        'operator definitions follow
        'type conversions definitions follow
    End Structure
End Module

All is fine except some default printouts I'm not satisified with and want to change them. Is it possible?

If I declare a variable:

Dim ID As New DbKey(12)

Then in immediate pane:

Case 1:

? String.Format("{0}", ID))

Result:

Application1.DbKeyModule+DbKey

Case 2:

? ID

Result:

ID = 12
    _Value: 12
    Null: 0
    Value: 12

Can be one or both these two default outputs changed to something else?

Note: Expressions like "Value is " & ID or ID.ToString() work correctly because I declared necessary methods, operators and typecasts. But the above two expressions are referring to some object-to-string form beyond the control of these means. Can it be changed?


Solution

  • After all I guess the printouts are specific product of

    • String.Format() method in Case 1
    • Debug.Print() method in Case 2

    and therefore they cannot be changed.

    Didn't take time to verify in reference source though.