473,395 Members | 1,931 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,395 software developers and data experts.

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 7637
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).FullName == "xxx")
{
list.Add(new a<int>());
list.Add(new a<string>());
}
}

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

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-pleasedevexpress.comwrote in message news:c1**************************@news.microsoft.c om...
>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).FullName == "xxx")
{
list.Add(new a<int>());
list.Add(new a<string>());
}
}
void Cascade()
{
foreach(a<titem in list)
item.DoSomething();
}
}
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-pleasedevexpress.comwrote in message
news:c1**************************@news.microsoft.c om...
>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).FullName == "xxx")
{
list.Add(new a<int>());
list.Add(new a<string>());
}
}
void Cascade()
{
foreach(a<titem in list)
item.DoSomething();
}
}
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-pleasedevexpress.coma écrit dans le
message de news: c1**************************@news.microsoft.com...

| 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**************@TK2MSFTNGP02.phx.gbl...
"Dustin Campbell" <du*****@no-spam-pleasedevexpress.coma écrit dans le
message de news: c1**************************@news.microsoft.com...

| 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
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...
4
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>....
8
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
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...
0
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...
4
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...
7
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...
10
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...
2
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,...
11
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...
0
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...
0
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,...

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.