2
votes

I did search for this first and there are some similar posts, but i couldn't find my answer. :(

Lets say I have a Class:

Public Class ND
    Private mNumsDrawn = New List(Of Integer)

    Public Sub New(ByVal vNum As Integer)
        For i = 1 To vNum
            mNumsDrawn.Add(0)
        Next
    End Sub

    Public Property NumsDrawn As List(Of Integer)
        Get
            Return mNumsDrawn
        End Get
        Set(ByVal value As List(Of Integer))
            mNumsDrawn = value
        End Set
    End Property
End Class

I want to override the .ToString of the Property NumsDrawn.

If I put the .ToString override in the class then its just overriding the class' .ToString of course. I could use this to output all the numbers in the list, but I want to change the output of one number in the draw.

Dim MyDraw = New ND(6)

MsgBox(MyDraw.ToString)

But what I want is something like this..

MsgBox(MyDraw.NumsDrawn(1).ToString)

Thanks!

2
What is wrong with MyDraw.NumsDrawn(1).ToString() ?Alex B.
You'll have to derive your own class from List(Of T). That's possible. Why you want to pass an argument to NumsDrawn is however very hard to guess.Hans Passant
I actually haven;t used Lists before and this was all set as arrays. I'm passing an argument to Numsdrawn New because the size needs to be set and it can be different each time used, but then doesn't change. I'll probably go back to arrays since I'm a little more familiar with them and the numbers don't change once set. The Class ND is also just a subclass of a larger class, I tried to simplify the question. y main question isn't even just .Tostring, its more how to create a Method for a Property in a class. I'll work on the just creating another class way and see where it goes, thanks!Konrad
Alex, I wanted to override the NumsDrawn().ToString so I could Pad the integers with zeros. But more General question is how to create a method of a property in a class (not proper wording probably). Something like MyDraw.NumsDrawn(1).MyRoutine. Anything I put in ND class will come out as Mydraw.MyRoutineKonrad

2 Answers

2
votes

What you are asking for is overriding the ToString() method of ND. Although this is possible, it is IMO not really helpfull.

What will be more helpful is to define an extension method on ND. Notice that an extension method needs to be placed in a Public Module (which is effectively a static class).

This can be done as follows:

<Extension>
Public Function ToFormattedValue(instance As ND, index As Integer) As String
    Return instance.NumsDrawn(index).ToString()
End Function

' Use as:
MessageBox.Show(MyDraw.ToFormattedValue(1))

If you still want to override the ToString method, the code is almost the same. It is not really overriding it just adding an overload. Like this:

Public Class ND
    ' Other code here
    Public Function ToString(index as Integer) As String
        Return Me.NumsDrawn(1).ToString
    End Function
End Class
1
votes

I'm not sure why you would want to have a custom .ToString() override on an Integer type ...but...

A simple way to do this would be to change your NumsDrawn from a List(Of Integer) to a List(Of NumClass) and then override the .ToString on that class:

Public Class ND
    Private mNumsDrawn As List(Of NumClass) = New List(Of NumClass)

    Public Sub New(ByVal vNum As Integer)
        For i = 1 To vNum
            mNumsDrawn.Add(New NumClass(0))
        Next
    End Sub

    Public Property NumsDrawn As List(Of NumClass)
        Get
            Return mNumsDrawn
        End Get
        Set(ByVal value As List(Of NumClass))
            mNumsDrawn = value
        End Set
    End Property
End Class

Public Class NumClass
    Public Property Value As Integer
    Public Sub New(value As Integer)
        Me.Value = value
    End Sub
    Public Overrides Function ToString() As String
        Return "Here is my formatted value: " & Me.Value
    End Function
End Class

Usage:

    Dim myND As New ND(99)
    Debug.WriteLine(myND.NumsDrawn(6).ToString()) 'outputs:- Here is my formatted value: 0