Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add class to listbox

Status
Not open for further replies.

mgriffith

MIS
Jul 3, 2001
177
US
i have created a simple class and want to add it to a listbox using the overloaded tostring() method...i've seen this done in about 10 examples, and straight from msdn, but it's not working for me (using a System.Web.UI.WebControls.ListBox)

even with a simple class:
-------------------
| Public Class asdf
| Public Overrides Function ToString() As String
| Return "asdf"
| End Function
| End Class
-------------------
i try to call
-------------------
| lstboxMyListBox.Items.Add(new asdf())
-------------------
or even
-------------------
| cstr(new asdf())
-------------------
and it won't let it compile because:
"Value of Type 'mynamespace.asdf' cannot be converted to 'String'"

i then tried this:
-------------------
| Public Class asdf
| Public Overloads Overrides Property ToString() As String
| Get
| Return "asdf"
| End Get
| Set(ByVal Value As String)
|
| End Set
| End Property
| End Class
-------------------
but i get an error compiling the class because:
"'ToString' conflicts with a function by the same name declared in 'Object'"

any ideas?
mike griffith
----------------------------
mgriffith@lauren.com
mdg12@po.cwru.edu
 
I haven't leared VB.Net too much yet, but I use VB 6 and Java a lot, which .Net kind of takes concepts from both. Now, as far as non syntaxual advice, when you make objects in Java, I would say you have to called the Class AND the method.

Such as ...Items.Add(asdf.methodName())

I'm sure there are others here who can give you exact syntax, and I may even be way off base with .Net.
 
if i was to do that, then only the returned object (in my case a string) would be added. i need to access the other methods and properties of the object (though the list) after i have added them to the list collection

i actually figured this one out kind of (i'm still in the process of refinement). what i did was fill up an arraylist with my objects

then i set the datasource of the listbox to my arraylist...then i executed the databind() method and whalla! it worked. the only problem's i'm having now is maintaining state though requests in the arraylist...i'm temporarily solving it through the use of the session object to store the arraylist, but that's not going to last. mike griffith
----------------------------
mgriffith@lauren.com
mdg12@po.cwru.edu
 
Oh, so you are populating the list with attributes of your class? I misunderstood, and figured you were passing a string to your class somehow and only wanting those strings in yoru list.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top