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. Convert list (of myclass) to ienumerable

Convert list (of myclass) to ienumerable

Scheduled Pinned Locked Moved Visual Basic
7 Posts 3 Posters 1 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.
  • J Offline
    J Offline
    JR212
    wrote on last edited by
    #1

    Hi, I've created multiple lists of my own class. Now I want to add them to a listview. with Always the same sub. So I was thinking convert it to an ienumerable and than walk throu it. however I can't find a way to do it. Anybody has an idee to fill my listview. Jan

    D 1 Reply Last reply
    0
    • J JR212

      Hi, I've created multiple lists of my own class. Now I want to add them to a listview. with Always the same sub. So I was thinking convert it to an ienumerable and than walk throu it. however I can't find a way to do it. Anybody has an idee to fill my listview. Jan

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      List already implements IEnumberable. There's nothing to convert. Perhaps if you showed some examples of what you're talking about?

      A guide to posting questions on CodeProject

      Click this: Asking questions is a skill. Seriously, do it.
      Dave Kreskowiak

      J 1 Reply Last reply
      0
      • D Dave Kreskowiak

        List already implements IEnumberable. There's nothing to convert. Perhaps if you showed some examples of what you're talking about?

        A guide to posting questions on CodeProject

        Click this: Asking questions is a skill. Seriously, do it.
        Dave Kreskowiak

        J Offline
        J Offline
        JR212
        wrote on last edited by
        #3

        Hi, I added 2 pictures [Picture 1](http://users.telenet.be/janr/1.PNG) and [Picture 2](http://users.telenet.be/janr/2.PNG) Both are from different classes. i want them to load into the same listview with the same subroutine(sub sFillListview(c as collection)) or something like that. Prefereble with headers. Like you can see picture 1 has only 2 columns but picture 2 has 23 thanks already for looking Jan

        R D 2 Replies Last reply
        0
        • J JR212

          Hi, I added 2 pictures [Picture 1](http://users.telenet.be/janr/1.PNG) and [Picture 2](http://users.telenet.be/janr/2.PNG) Both are from different classes. i want them to load into the same listview with the same subroutine(sub sFillListview(c as collection)) or something like that. Prefereble with headers. Like you can see picture 1 has only 2 columns but picture 2 has 23 thanks already for looking Jan

          R Offline
          R Offline
          Ralf Meier
          wrote on last edited by
          #4

          If you want to do a kind of automation you need something what your different classes have in common. You should provide much more details - perhaps there is a solution ...

          1 Reply Last reply
          0
          • J JR212

            Hi, I added 2 pictures [Picture 1](http://users.telenet.be/janr/1.PNG) and [Picture 2](http://users.telenet.be/janr/2.PNG) Both are from different classes. i want them to load into the same listview with the same subroutine(sub sFillListview(c as collection)) or something like that. Prefereble with headers. Like you can see picture 1 has only 2 columns but picture 2 has 23 thanks already for looking Jan

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            Your classes have nothing in common, so your "single method to do it all" idea won't work. So, how is your "one method to rule them all" supposed to know how to handle each different class passed in the parameters? Are you looking to take the data from these classes and format it somehow to be added to a ListView? If that's the case, you have to add something to these classes to give something in common. Create an Interface that both of these classes implements so each class can supply code to format itself to be used in a ListView:

            Public Interface IListViewFormat
                Function ListViewFormat() As String
            End Interface
            

            Now, each class has to implement the interface and supply the code for the ListViewFormat method.

            Public Class Aansluiting Implements IListViewFormat
                ... properties code ...
            
                Public Function ListViewFormat()
                    return String.Format("{0} - {1}", ID, aansluiting)
                End Function
            

            Then you can change your List to be a collection of IListViewFormat and pass that to your method that adds the items to a ListView.

            Dim collection = New List(Of IListViewFormat)
            ... add your items to the List and pass it to the method
            Public Sub AddToListView(ByVal collection As IList(Of IListViewFormat))
                For Each item In collection
                    ListView1.Items.Add(item.ListViewFormat)
                Next
            End Sub
            

            A guide to posting questions on CodeProject

            Click this: Asking questions is a skill. Seriously, do it.
            Dave Kreskowiak

            J 1 Reply Last reply
            0
            • D Dave Kreskowiak

              Your classes have nothing in common, so your "single method to do it all" idea won't work. So, how is your "one method to rule them all" supposed to know how to handle each different class passed in the parameters? Are you looking to take the data from these classes and format it somehow to be added to a ListView? If that's the case, you have to add something to these classes to give something in common. Create an Interface that both of these classes implements so each class can supply code to format itself to be used in a ListView:

              Public Interface IListViewFormat
                  Function ListViewFormat() As String
              End Interface
              

              Now, each class has to implement the interface and supply the code for the ListViewFormat method.

              Public Class Aansluiting Implements IListViewFormat
                  ... properties code ...
              
                  Public Function ListViewFormat()
                      return String.Format("{0} - {1}", ID, aansluiting)
                  End Function
              

              Then you can change your List to be a collection of IListViewFormat and pass that to your method that adds the items to a ListView.

              Dim collection = New List(Of IListViewFormat)
              ... add your items to the List and pass it to the method
              Public Sub AddToListView(ByVal collection As IList(Of IListViewFormat))
                  For Each item In collection
                      ListView1.Items.Add(item.ListViewFormat)
                  Next
              End Sub
              

              A guide to posting questions on CodeProject

              Click this: Asking questions is a skill. Seriously, do it.
              Dave Kreskowiak

              J Offline
              J Offline
              JR212
              wrote on last edited by
              #6

              Seems that there is no direct solution. I have added another function that return a simple collection. In item 1 I return fieldnames, fieldtypes and fieldsizes. The other items contains the real elements. that way I have all I want. Thanks for thinging with me. Jan

              R 1 Reply Last reply
              0
              • J JR212

                Seems that there is no direct solution. I have added another function that return a simple collection. In item 1 I return fieldnames, fieldtypes and fieldsizes. The other items contains the real elements. that way I have all I want. Thanks for thinging with me. Jan

                R Offline
                R Offline
                Ralf Meier
                wrote on last edited by
                #7

                The suggestion of Dave is the direct Solution ... If your classes have nothing in common you have to create something. This could be an interface. Now you are able (by reflection) to detect those classes which have this interface and put them (or a content from them) into a List.

                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