473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

tostring

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

Nov 20 '05 #1
6 1349
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
"steve" <a@b.com> wrote in message
news:10*************@corp.supernews.com...
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

Nov 20 '05 #2
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

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OQ*************@TK2MSFTNGP11.phx.gbl...
| 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
|
|
| "steve" <a@b.com> wrote in message
| news:10*************@corp.supernews.com...
| > 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
| >
| >
| >
|
|
Nov 20 '05 #3
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" <a@b.com> wrote in message
news:10*************@corp.supernews.com...
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

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OQ*************@TK2MSFTNGP11.phx.gbl...
| 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
|
|
| "steve" <a@b.com> wrote in message
| news:10*************@corp.supernews.com...
| > 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
| >
| >
| >
|
|

Nov 20 '05 #4
"steve" <a@b.com> schrieb
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

Nov 20 '05 #5
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
Nov 20 '05 #6
i didn't...you can't...i tried them independently. got it working fine now.

thanks for the response though.

steve
Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: BBFrost | last post by:
I'm receiving decimal values from database queries and placing them on a report page. The users want to see the following .... Db Value Display Value 123.3400 123.34...
6
by: Joe | last post by:
Stupid question, but I'm really stuck I have a class that overrides ToString(). When this class is cast back to Object, and ToString() called, Object.ToString() is called instead. I've tried...
8
by: John Cobb | last post by:
Excuse me if I use some terminology incorrectly as I am not well versed in Object Orientation. In short I want to create an Enumerated type similar to the following Enum Monitor as Byte Small...
0
by: bcobra | last post by:
What am I doing wrong? the code: age Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <%@ Register TagPrefix="MM" Namespace="DreamweaverCtrls"...
7
by: Dylan Parry | last post by:
Hi folks, I was wondering if there is any way of formatting a date to include the ordinal characters? I've looked at the documentation for DateTime.ToString(), but no where can I find...
2
by: archana | last post by:
Hi all, I am facing some wired problem while using above mention data type. What i am doing is i am writing DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" +...
1
by: ivansh | last post by:
Hello, For one java class (Hello) i use another (HelloPrinter) to build the string representation of the first one. When i've tried to use this from within jython, HelloPrinter.toString(hello)...
8
by: Phil Jollans | last post by:
Hi, I am having difficulty overriding the ToString() method of CultureInfo using Visual Studio 2005. Exactly the same code works fine with Visual Studio .NET 2003. What I am doing is adding...
2
by: Berryl Hesh | last post by:
I'm interested in how experienced.Net developers might handle routine display tasks as a general strategy. Let's say you have a use in your domain for value objects that encapsulate a person's...
19
by: Michael C | last post by:
If we have something like this object x = null; MessageBox.Show(x.ToString()) we get an error. That kindof makes sense but there is no reason the behaviour couldn't be to return an empty...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.