473,804 Members | 3,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

List of a generic type

Hi all,

Is it possible to have a collection of a generic type?

for example:

class a<twhere t : class, new
{
}

class b
{
List<a<t>p;
}

I know that the previous example does not work, but any ideas on how to make
that concept work?

I need a collection that stores objects that are of a generic type.

Jorge Varas
Nov 28 '06 #1
6 7663
Is it possible to have a collection of a generic type?
>
for example:

class a<twhere t : class, new
{
}
class b
{
List<a<t>p;
}

I know that the previous example does not work, but any ideas on how
to make that concept work?

I need a collection that stores objects that are of a generic type.
Sure, you can do that by defining the same generic parameter on class b:

class a<twhere t : class, new()
{
}
class b<twhere t : class, new()
{
List<a<t>p;
}

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 28 '06 #2
Sorry, my bad, I didn't explained the problem with enough detail.

class a<twhere t: class, new
{
void DoSomething()
{
}
}

class b<uwhere u: class, new
{
List<a<v>list;

void setup()
{
if (typeof(u).Full Name == "xxx")
{
list.Add(new a<int>());
list.Add(new a<string>());
}
}

void Cascade()
{
foreach(a<titem in list)
item.DoSomethin g();
}
}

So u and v are different types, the fact that b is instanciated with a type t1 does not define that the list is going to be of type a<t1>. Actually, the list might contain instantiations of 'a' with different types under different cases

BTW. I know that the previous example does not work... but it ilustrate the problem that I have.

"Dustin Campbell" <du*****@no-spam-pleasedevexpres s.comwrote in message news:c1******** *************** ***@news.micros oft.com...
>Is it possible to have a collection of a generic type?

for example:

class a<twhere t : class, new
{
}
class b
{
List<a<t>p;
}

I know that the previous example does not work, but any ideas on how
to make that concept work?

I need a collection that stores objects that are of a generic type.
Sure, you can do that by defining the same generic parameter on class b:

class a<twhere t : class, new()
{
}
class b<twhere t : class, new()
{
List<a<t>p;
}

Best Regards,
Dustin Campbell
Developer Express Inc.

Nov 28 '06 #3
Sorry, my bad, I didn't explained the problem with enough detail.
>
class a<twhere t: class, new
{
void DoSomething()
{
}
}
class b<uwhere u: class, new
{
List<a<v>list;
void setup()
{
if (typeof(u).Full Name == "xxx")
{
list.Add(new a<int>());
list.Add(new a<string>());
}
}
void Cascade()
{
foreach(a<titem in list)
item.DoSomethin g();
}
}
So u and v are different types, the fact that b is instanciated with a
type t1 does not define that the list is going to be of type a<t1>.
Actually, the list might contain instantiations of 'a' with different
types under different cases

BTW. I know that the previous example does not work... but it
ilustrate the problem that I have.
So, if I understand correctly, you want to be able to use a generic type
without actually closing it. E.g., like this:

class b<uwhere u: class, new()
{
List<a<>list;
}

Correct? If so, you can't do that with C#.

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 28 '06 #4
I can do it using objects and the checking if the type of the object was
constructed from the generic class a, but I hoped for a more elegant
solution.

Thanks for your clarification.

"Dustin Campbell" <du*****@no-spam-pleasedevexpres s.comwrote in message
news:c1******** *************** ***@news.micros oft.com...
>Sorry, my bad, I didn't explained the problem with enough detail.

class a<twhere t: class, new
{
void DoSomething()
{
}
}
class b<uwhere u: class, new
{
List<a<v>lis t;
void setup()
{
if (typeof(u).Full Name == "xxx")
{
list.Add(new a<int>());
list.Add(new a<string>());
}
}
void Cascade()
{
foreach(a<tite m in list)
item.DoSomethi ng();
}
}
So u and v are different types, the fact that b is instanciated with a
type t1 does not define that the list is going to be of type a<t1>.
Actually, the list might contain instantiations of 'a' with different
types under different cases

BTW. I know that the previous example does not work... but it
ilustrate the problem that I have.

So, if I understand correctly, you want to be able to use a generic type
without actually closing it. E.g., like this:

class b<uwhere u: class, new()
{
List<a<>list;
}

Correct? If so, you can't do that with C#.

Best Regards,
Dustin Campbell
Developer Express Inc.


Nov 28 '06 #5
"Dustin Campbell" <du*****@no-spam-pleasedevexpres s.coma écrit dans le
message de news: c1************* *************@n ews.microsoft.c om...

| Correct? If so, you can't do that with C#.

....unless you insert a non-generic base class before the generic one.

class b
{
}

class b<T: b
{
}

then you can have a List<b>, but you will have to determine what types the
items really are before you deal with them.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Nov 28 '06 #6
..Thanks! this works for my situation

public abstract class lc {
public abstract void Cascade();
}

public class lvc<T: lc where T : class, new() {
public override void Cascade() {
}
}

public class a {

List<lclist;

void SetUp() {
list.Add(new lvc<int>());
list.Add(new lvc<string>());
}

public void DoSomething() {
foreach(lc item in list)
item.Cascade();
}
}

I added the full solution so it is left in the archive... it bothers me to be looking for a solution to a problem just to find a posting saying "Thanks!, I solved" and nothing more...
"Joanna Carter [TeamB]" <jo****@not.for .spamwrote in message news:Oz******** ******@TK2MSFTN GP02.phx.gbl...
"Dustin Campbell" <du*****@no-spam-pleasedevexpres s.coma écrit dans le
message de news: c1************* *************@n ews.microsoft.c om...

| Correct? If so, you can't do that with C#.

...unless you insert a non-generic base class before the generic one.

class b
{
}

class b<T: b
{
}

then you can have a List<b>, but you will have to determine what types the
items really are before you deal with them.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Nov 28 '06 #7

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

Similar topics

6
4603
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any potential pitfalls I'd be extremely grateful. Cheers Steve
4
53018
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>. However, I need to cast that list to a List<BaseClass> and it is not working. The code is below. I get the following exception: "Unable to cast object of type 'System.Collections.Generic.List`1' to type 'System.Collections.Generic.List`1'." ...
8
4965
by: Vivek | last post by:
Hi, I wish to update the LIST<T> created in PARENT FORM from the CHILD FORM. Currently I have declared the LIST<Role> as public in my parent form. What can I do to update the <LIST>? Thanks
4
2821
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool RuleDelegate<T>(T item); In my class my goal is to use a generic list collection to contain my generic delegates. This will allow me to pass this List to another library and call this list of functions. This could provide a new way to build rule base...
0
5930
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting problem serializing my stuff to XML when it comes to collections. I'm currently using .net2 with generic lists to prevent users putting all sorts of stuff in the arrays (Although im sure i'll be the only user of the classes but not the game, anyway)....
4
1438
by: J2EE Convert | last post by:
Hello, Sorry if this has already been beaten to death. However, I was not able to find the answer in my searches. Is there any performance trade off between creating and using a generic list of a Class Type and a generic list of a Struct Type assuming both types are the same save for one being a structure and one being a class. See below. List<StructAmList; List<ClassAmList;
7
1441
by: DippyDog | last post by:
My problem is the subject line. Basically, my thinking was that, given class TestSpec1 inherits from class TestBase, since I can do this... Dim tst As TestBase = GetTestObj() Function GetTestObj() As TestSpec1 Return New TestSpec1 End Function
10
6472
by: lpinho | last post by:
Hi all, I have a class (named for the example myObject) that can be of several types (int, string, float, etc), instead of using a object to define it's type I used a generic. public class MyObject<T: ChangeObject { ... public MyObject(string name, T value)
2
6173
by: Fred Mellender | last post by:
I am trying to use reflection to output the fields (names and values) of an arbitrary object -- an object dump to a TreeView. It works pretty well, but I am having trouble with generic lists, like List<char>. What I have working is : Type type = newObj.GetType(); //newObj is what I'm trying to dump
11
2556
by: Scott Stark | last post by:
Hello, The code below represents a singly-linked list that accepts any type of object. You can see I'm represting the Data variable a System.Object. How would I update this code to use generics instead of System.Object. I want the code in Form1_Load to remain exactly the same, but in the background I want to use generics. I'm trying to get a better understanding of how it works and I'm a little stuck.
0
10323
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
10311
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
9138
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...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
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
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2988
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.