Populating Generic Lists

  • Thread starter Thread starter Andrew Ducker
  • Start date Start date
A

Andrew Ducker

I have something I'm trying to make work with generics, and it seems
like it should, but I can't quite get there.

I have a subclass of List<T> that I want to populate automatically.
Each different T is populated with a different set of data. Some
sample code is given below - which clearly doesn't work, because you
can't translate back and forth between the types.

Can anyone tell me the right way to solve this kind of problem?

public static List<T> GetPopulatedList<T>()
{
List<T> myList = new List<T>();
if (typeof(T) == typeof(Dog))
{
PopulateListWithDogs(myList);
}
if (typeof(T) == typeof(Cat))
{
PopulateListWithCats(myList);
}
}

private static void PopulateListWithDogs(List<Dog> dogList)
{
dogList.Add(new Dog("Labrador"));
dogList.Add(new Dog("Alsatian"));
}

private static void PopulateListWithCats(List<Cat> catList)
{
catList.Add(new Cat("Siamese"));
catList.Add(new Cat("Persian"));
}

All I want to do is pass in a type and get a list back of all the
possible options for that type. (And no, my actual app doesn't work
with cats and dogs, this is just an example)

Cheers for any help you can give,

Andy
 
Andrew Ducker said:
I have something I'm trying to make work with generics, and it seems
like it should, but I can't quite get there.

I have a subclass of List<T>

Why?

that I want to populate automatically.

How?
Each different T is populated with a different set of data. Some
sample code is given below - which clearly doesn't work, because you
can't translate back and forth between the types.

Aha.

I think what you are doing is not enough for maintainers.
We all know maintainers get a kick from a challange.

Hence, you should install the preview compiler of C#3. And then solve your
problem by adding extentions on interfaces, and then subclass, where the
implementing classes should be in a different assembly carrying
namespaces like System.Util and System.Extentions.

While you still are doing pure .NET, please make sure to sport unsafe
methods, not a single one as that would be too easy to find, but like almost
everywhere. This will ensure that the deployer and later the maintainer will
have some fun with the GAC.

Oh, no maintainer will respect you if refuse to do PInvoke on User32 and
Kernel32. You don't need to call but a few win32 function, heck it might not
even make sense, call them anyways. Make sure your app depend on them! More
advanced is going via COM and require VBRUN4.DLL. That will make for a
laugh.

If the only tool you got is generics, then everything starts to look like a
type.
- Michael S
 
Michael said:
that I want to populate automatically.

How?

That's what I was asking. So long as I have a simple method of getting
to a set of populated Lists I don't mind how it's done. It's currently
being done by having 120 subclasses of ArrayList, each of which casts
to the right type in the various accessor methods (which means whenever
a new one is added the old code is copied and pasted, and then the
return types are all changed). I can replace all of this with a single
generic List. However, at the moment each type also populates itself -
and that's the bit I'm having problems with.

Andy
 
That's what I was asking. So long as I have a simple method of getting
to a set of populated Lists I don't mind how it's done. It's currently
being done by having 120 subclasses of ArrayList, each of which casts
to the right type in the various accessor methods (which means whenever
a new one is added the old code is copied and pasted, and then the
return types are all changed). I can replace all of this with a single
generic List. However, at the moment each type also populates itself -
and that's the bit I'm having problems with.

Andy

I won't touch this with a sixty foot pole.

Sorry Andy. And I hope you forgive me for having a joke at your post.

Happy In Hiding
- Michael
 
Andrew,
I have something I'm trying to make work with generics, and it seems
like it should, but I can't quite get there.

You just have to change

PopulateListWithDogs(myList);

to

PopulateListWithDogs(myList as List<Dog>);


Mattias
 
"Andrew Ducker" <[email protected]> a écrit dans le message de (e-mail address removed)...

| That's what I was asking. So long as I have a simple method of getting
| to a set of populated Lists I don't mind how it's done. It's currently
| being done by having 120 subclasses of ArrayList, each of which casts
| to the right type in the various accessor methods (which means whenever
| a new one is added the old code is copied and pasted, and then the
| return types are all changed). I can replace all of this with a single
| generic List. However, at the moment each type also populates itself -
| and that's the bit I'm having problems with.

A Generic class is just what it says on the tin, a generic class.

If you are doing anything that is type specific, then that code doesn't
belong in the generic class at all. You don't need to subclass List<T> to
achieve what you want, but you will not get away with one sensible class, if
you are going to populate lists of different objects created manually as you
suggest, then you are going to have to create one class per type.

public static class DogPopulator
{
void static Populate(List<Dog> list)
{
list.Add(new Dog("Labrador"));
list.Add(new Dog("Alsatian"));
}
}

{
List<Dog> list = new List<Dog>();

DogPopulator.Populate(list);

...
}

Joanna
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top