473,783 Members | 2,286 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<TGetPopula tedList<T>()
{
List<TmyList = new List<T>();
if (typeof(T) == typeof(Dog))
{
PopulateListWit hDogs(myList);
}
if (typeof(T) == typeof(Cat))
{
PopulateListWit hCats(myList);
}
}

private static void PopulateListWit hDogs(List<Dogd ogList)
{
dogList.Add(new Dog("Labrador") );
dogList.Add(new Dog("Alsatian") );
}

private static void PopulateListWit hCats(List<Catc atList)
{
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 2939
"Andrew Ducker" <an****@ducker. org.ukwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.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.Extentio ns.

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.goo glegroups.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.goog legroups.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

PopulateListWit hDogs(myList);

to

PopulateListWit hDogs(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************* *********@i3g20 00...legro 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.

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

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

DogPopulator.Po pulate(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
2489
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 aspx page. But the problem is these tend to really slow the development -- it takes up to 15 seconds for the page to come up in VS.NET design environment, so I'm thinking about taking these out and populating the controls dynamically using the...
11
14521
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 have in the list. With my array approach, I have to manage re-sizing the array myself. So, is using List<myClass> x as fast as MyClass x ?
2
1978
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 "transform" which works not only on lists, but lists of list, lists of lists of lists, etc. I'm calling it "fmap" and it passes around actual lists instead of iterators (for now). In order to get it to work, I thought I'd have one templated...
0
6854
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
2262
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) The parent form have a custom control, which have a public sub looking
0
1706
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 SQL statements in the Data Acess Layer to create more secure web service application. The Web service class is also in the Data Access Layer. I need to populates the 4 Dropdown list boxes on the web form from one table. I have Customer table which...
1
1280
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 lists on a form and want to use a single piece of code to display each one by passing the relevant query to the code. My problem is that I dont know which mysql_fetch command to use because I cannot access each item by name only number (as the name...
1
2785
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, List<VtheSecondList); I pass the name of the method, the arguments for the "GenericMethodWithGenericArguments" to another method, which is supposed to invoke this method using the Invoke method in the MethodInfo class. My process of invocation is as follows
1
2844
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 server to the client. Whould it be better to use generic lists? Jeff
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10083
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.