Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

Passing several typesafe Lists to a method?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Adam M - 27 Aug 2007 21:36 GMT
Hi all,

What is the syntax for a method that accepts a list of generic lists?

For example, I have a List that contains the following lists:

List<Car>
List<Boat>
List<Plane>

I want to pass this "list of lists" to a method so the list contents can be
persisted. For example, I am trying to do something like this (but I am lost
for the right syntax):

SaveObjectsToDisk(List<T> lists)
{
foreach(List list in lists)
{
foreach(Type T in list) db.Save(T);
}
}

Thanks!
Adam M - 28 Aug 2007 15:24 GMT
Just a ping to see if anyone has any ideas on this one...

Thanks!

> Hi all,
>
[quoted text clipped - 19 lines]
>
> Thanks!
Nicholas Paldino [.NET/C# MVP] - 28 Aug 2007 17:13 GMT
If the method is going to be generic (in the sense that you will use it
for lists of lists of multiple types, and not always for Car, Boat, and
Plane lists) then the only way I can see this happening is if you use
reflection.

   You can't use List<List<T>> since that would fix all the lists.

   Rather, you should take an IList (the non-Generic version) and then
cycle through those, making sure that each element is an IList<T>.  Use
reflection to find out what <T> is and then call the appropriate save
method.

   Your example won't compile, btw, since you are trying to declare an
instance variable T when you have a type parameter of the same name.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hi all,
>
[quoted text clipped - 21 lines]
>
> Thanks!
bob - 30 Aug 2007 05:54 GMT
Hi Adam,
Assuming examples below were not just accidentally related how about
using  a list of the base class 'Vehicle'.
Leave it to the DAL to figure out the saving technique.
regards
Bob

>Hi all,
>
[quoted text clipped - 19 lines]
>
>Thanks!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.