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