Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. add object to listbox

add object to listbox

Scheduled Pinned Locked Moved Visual Basic
comdesignhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mgriffith
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • M mgriffith

      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

      C Offline
      C Offline
      Colin Leitner
      wrote on last edited by
      #2

      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()

      M 1 Reply Last reply
      0
      • C Colin Leitner

        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()

        M Offline
        M Offline
        mgriffith
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups