tostring

  • Thread starter Thread starter steve
  • Start date Start date
S

steve

i'm trying to create an item to be added to a listbox. reason being is that
the "item" has properties i need to access when it is selected. i'm having
trouble displaying it in the list though. i've tried putting a public
overrides function ToString() and a public shadows function ToString() as
one of its interfaces so that the listbox knows what text to display. the
items should simply be diplaying either an item number and/or description
based on other properties of my item.

where have i gone wrong?

tia,

steve
 
Steve,
Are you putting a "Public Overrides Function ToString() As String" in your
class?

The following should work:

Option Strict On

Public Class MyItem

Private ReadOnly m_text As String

Public Sub New(ByVal text As String)
m_text = text
End Sub

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

...

End Class

Then within your Form, you need something like:

Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
With ListBox1.Items
.Add(New MyItem("item 1"))
.Add(New MyItem("item 2"))
.Add(New MyItem("item 3"))
.Add(New MyItem("item 4"))
.Add(New MyItem("item 5"))
End With
End Sub

Note that you need to include the "As String" on the function otherwise
VB.NET will assume As Object, which is not at all what you want. Also the
Option Strict On, will help ensure that you include the type on the
Function.

Hope this helps
Jay
 
yes...sorry i left that out of the explanation. but, yes, i am declaring it
exactly as you've typed it below.

any ideas?

tia,

steve



| Steve,
| Are you putting a "Public Overrides Function ToString() As String" in your
| class?
|
| The following should work:
|
| Option Strict On
|
| Public Class MyItem
|
| Private ReadOnly m_text As String
|
| Public Sub New(ByVal text As String)
| m_text = text
| End Sub
|
| Public Overrides Function ToString() As String
| Return m_text
| End Function
|
| ...
|
| End Class
|
| Then within your Form, you need something like:
|
| Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
| With ListBox1.Items
| .Add(New MyItem("item 1"))
| .Add(New MyItem("item 2"))
| .Add(New MyItem("item 3"))
| .Add(New MyItem("item 4"))
| .Add(New MyItem("item 5"))
| End With
| End Sub
|
| Note that you need to include the "As String" on the function otherwise
| VB.NET will assume As Object, which is not at all what you want. Also the
| Option Strict On, will help ensure that you include the type on the
| Function.
|
| Hope this helps
| Jay
|
|
| | > i'm trying to create an item to be added to a listbox. reason being is
| that
| > the "item" has properties i need to access when it is selected. i'm
having
| > trouble displaying it in the list though. i've tried putting a public
| > overrides function ToString() and a public shadows function ToString()
as
| > one of its interfaces so that the listbox knows what text to display.
the
| > items should simply be diplaying either an item number and/or
description
| > based on other properties of my item.
| >
| > where have i gone wrong?
| >
| > tia,
| >
| > steve
| >
| >
| >
|
|
 
Steve,
The code I showed works, can you post the minimum amount of your code that
demonstrates the problem (15 - 20 lines).

Plus how you are setting & filling the listbox itself (no real need to post
the full code for the form itself.

I would not expect more then 15 to 20 lines of code.

Hope this helps
Jay
 
steve said:
i'm trying to create an item to be added to a listbox. reason being
is that the "item" has properties i need to access when it is
selected. i'm having trouble displaying it in the list though. i've
tried putting a public overrides function ToString() and a public
shadows function ToString() as one of its interfaces so that the
listbox knows what text to display. the items should simply be
diplaying either an item number and/or description based on other
properties of my item.

where have i gone wrong?

I don't fully understand: How can you add an "Overrides" /and/ a "Shadows"
function with the same name?


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
thanks for the help jay...i had left a debug tag in the "items" code that
was supposed to not populate the list. duh!

i do appreciate your help though!

thanks,

steve
 
i didn't...you can't...i tried them independently. got it working fine now.

thanks for the response though.

steve
 
Back
Top