ToString Property for user-defined class/object

  • Thread starter Thread starter Vilem Sova
  • Start date Start date
V

Vilem Sova

I want to use a user-defined object in the 'Add' method to add items to a
combo box. The help documentation says that if you use an object in the add
method then the object's 'ToString' method is used to obtain the string to
display in the combo box.

I've added a public property "ToString" to my object that contains the
required text to display, but it is not being called to get the required
text. The text that is appearing in the combo box list is
ProjectName.ObjectName for every item in the list.

So how do I create the "ToString" property so that it's called by 'Add'
method of a combo box?

Thanks
Vilem Sova
 
Vilem,
So how do I create the "ToString" property so that it's called by 'Add'
method of a combo box?
You don't, you need to override the ToString Function in your class.

Something like:

Public Class UserDefined

Private m_value As String

Public Sub New(value As String)
m_value = value
End Sub

Public Overrides Function ToString() As String
Return m_value
End Function

End Public

Hope this helps
Jay
 
Back
Top