473,386 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Populating Generic Lists

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<Tthat 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<TGetPopulatedList<T>()
{
List<TmyList = new List<T>();
if (typeof(T) == typeof(Dog))
{
PopulateListWithDogs(myList);
}
if (typeof(T) == typeof(Cat))
{
PopulateListWithCats(myList);
}
}

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

private static void PopulateListWithCats(List<CatcatList)
{
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

Sep 6 '06 #1
5 2923
"Andrew Ducker" <an****@ducker.org.ukwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
>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




Sep 6 '06 #2

Michael S wrote:
"Andrew Ducker" <an****@ducker.org.ukwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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?
Actually, it doesn't have to be a subclass of List<Tfor the purposes
of this example.
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

Sep 6 '06 #3
"Andrew Ducker" <an****@ducker.org.ukwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
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

Sep 6 '06 #4
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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Sep 6 '06 #5
"Andrew Ducker" <an****@ducker.org.uka écrit dans le message de news:
11**********************@i3g2000cwc.googlegroups.c om...

| 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<Tto
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<Doglist)
{
list.Add(new Dog("Labrador"));
list.Add(new Dog("Alsatian"));
}
}

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

DogPopulator.Populate(list);

...
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Sep 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: msnews.microsoft.com | last post by:
I'd like to hear your thoughts on best methods for populating drop down list controls. I have states and countries drop down lists that don't change often, so naturally I "hard code" them in the...
11
by: ZenRhapsody | last post by:
Has anyone done any performance testing between new generic Lists and single dimensional arrays? I really like the code flexibility the List provides since I don't know how many items I will...
2
by: Greg Buchholz | last post by:
/* I've been experimenting with some generic/polytypic programs, and I've stumbled on to a problem that I can't quite figure out. In the program below, I'm trying to define a generic version of...
0
by: Wiktor Zychla [C# MVP] | last post by:
We do have generic classes, methods and delegates. My question is: what reason prevents us from having generic properties and indexers? // impossible public List<T> GetList<T> { get { ... }
3
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) ...
0
by: koonda | last post by:
Hi all, I have a Project due after one week. It is a web service project. I have a Web Form which communicates to the web service and this web service communicates to the database. I have all my...
1
Gyro
by: Gyro | last post by:
Hi all, I'm a php newbie and have read various posts/articles on populating select lists dynamically from a db table. However, I cant seem to get it working in the way I want... I have 3 select...
1
by: Suds | last post by:
Hi, I'm having an issue with invoking a Generic method that takes Generic Arguments. My method signature is public void GenericMethodWithGenericArguments<E, V>(List<EtheFirstList,...
1
by: Jeff | last post by:
..NET 2.0 Is generic lists faster then tradional lists when sending over a collection of objects (value by reference) in .NET remoting. Lets say if a list of object should be sent from a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.