add object to listbox
-
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 :~ michael griffith -------------------- mgriffith@lauren.com mdg12@po.cwru.edu
-
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 :~ michael griffith -------------------- mgriffith@lauren.com mdg12@po.cwru.edu
The first approach was the right one, but you have to use lstboxMyListBox.Items.Add(New Asdf().ToString()) Now this will work fine, but you cannot access the class anymore after that! I think you wanted to add a set of classes to a listbox and access these classes afterwords again and let the listbox use the ToString() function to display them correct, but listbox will only accept strings. You would have to use a collection too: Dim my_asdf As New Asdf() colMyCollection.Add my_asdf lstboxMyListBox.Items.Add my_asdf.ToString()
-
The first approach was the right one, but you have to use lstboxMyListBox.Items.Add(New Asdf().ToString()) Now this will work fine, but you cannot access the class anymore after that! I think you wanted to add a set of classes to a listbox and access these classes afterwords again and let the listbox use the ToString() function to display them correct, but listbox will only accept strings. You would have to use a collection too: Dim my_asdf As New Asdf() colMyCollection.Add my_asdf lstboxMyListBox.Items.Add my_asdf.ToString()
thanks for the reply....i actually was going about it wrong what i'm doing now (to my disguntlement) is keeping an arraylist of my objects in the session collection, and databinding to it...it's working fine i assumed that the ListBox.Items collection would hold objects (as long as they had a tostring() method), but i was wrong. you can, however databind to any object collection/list, as long as the tostring() method is provided, or a datavaluemember and datatextmember are specified michael griffith -------------------- mgriffith@lauren.com mdg12@po.cwru.edu