Solved

How can i create  a Generic collection of Generic type classes

Posted on 2008-10-24
22
740 Views
Last Modified: 2012-08-13
I am trying to create a generic collection of generic type classes as such...

public class QueryStringValues : System.Collection.Generic.List < nsMy.QueryStringValue < T> >

but the complier does not like it.

How can i define my generic class to the generic collection?
0
Comment
Question by:eddyevations
  • 10
  • 8
  • 4
22 Comments
 
LVL 18

Expert Comment

by:Priest04
ID: 22802407
I have never used this class, but according to MSDN documentation:

This class and its members are reserved for internal use and are not intended to be used in your code.

Namespace: Microsoft.Office.Server.Infrastructure
Assembly: Microsoft.Office.Server (in microsoft.office.server.dll)

Asside from being reserved, its not a generic class. So this is not possible

QueryStringValues<T>
0
 

Author Comment

by:eddyevations
ID: 22804013
Sorry, I do not follow..."QueryStringValues" is my own generic class.
0
 

Author Comment

by:eddyevations
ID: 22804080
I should have been more clear...i apologize.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22804595
So you want to inherit from List<T> then? If not, then the question is unclear. Maybe posting the code for using this class would be helpful.

// to inherit
public class QueryStringValues<T> : System.Collection.Generic.List <T>
0
 

Author Comment

by:eddyevations
ID: 22804794
This is my generic class...
public class MyClass<T>
{
    ...
}

I want to create a generic collection of MyClass<T> objects...
public class MyCollection : System.Collection.Generic.List < MyClass < T > >
{
}

0
 

Author Comment

by:eddyevations
ID: 22804802
btw...Thanks for responding Priest04
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22805262
You are welcome.


class MyClass<T> : List<T>

{

}
 

class MyCollection<T> : List<MyClass<T>>

{

}

Open in new window

0
 

Author Comment

by:eddyevations
ID: 22809675
I don't think that is what i am after...

I have have a generic class ( which is not a list ) something like...

public class MyClass<T>
{
    private string msName = null ;
    public string Name
    {
        get { return this.msName ; }
        set { this.msName = value ; }
    }

    private T moValue = default ( T )
    public T Value
    {
        get { return this.moValue ; }
        set { this.moValue = value; }
    }
}

I want a collection of MyClass < > objects that is not generic to the client...
public MyCollection : System.Collections.Generic.List < MyClass < > >
{
}

So i can create a new collection as follows...

private MyCollection oCollection = new MyCollection ( ) ;
oCollection.Add ( new MyClass < string > ( ) ) ;
oCollection.Add ( new MyClass < int > ( ) ) ;
0
 
LVL 23

Expert Comment

by:Tiggerito
ID: 22809951
I don't think you can do that.

Maybe you can get what you want by creating an interface to represent the common properties/methods of your items...
interfact IMyClass 

{

  public object CommonValue {get; set; };

  public void DoCommonThing();

  ...

}
 

public class MyClass<T> : IMyClass ...
 

public class MyCollection: List<IMyClass> ...

Open in new window

0
 

Author Comment

by:eddyevations
ID: 22811975
I already did that but it defeats the purpose of a strongly typed object and i do not see why i cannot do this.  The compiler allows you to declare MyClass<> for reflection.  It should allow me to add a Generic class to a collection.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22815596
eddyevations, look at the question you have posted (topic title). You wanted a generic question, and after receiving an example how to this, you say you don't want that. :) If I may ask you, I am pretty curious

what is the reason for not wanting to have a custom generic collection of MyClass<T>?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:eddyevations
ID: 22816292
Priest04,
Sorry, maybe the title is misleading, but i think what i am trying to do is clear and valuable.

How would i use the example you gave me..





public class MyClass<T> : List<T>

{

}

 

public class MyCollection<T> : List<MyClass<T>>

{

}
 

MyCollection <T> oCollection = new MyCollection < ??? > ;

Open in new window

0
 
LVL 18

Expert Comment

by:Priest04
ID: 22816410
Yes it is clear, although when we talk about value of it, Its arguable. I cant see any benefits in using strongly typed collection of generic classes, against using generic collection of generic classes.

You would use it like with any regular generic collection, passing a type among <> brackets.
MyCollection<MyClass<string>> oCollection = new MyCollection <MyClass<string>>();

Open in new window

0
 
LVL 18

Expert Comment

by:Priest04
ID: 22816454
I have only given an example of string, you will replace it with the corresponding type.
0
 

Author Comment

by:eddyevations
ID: 22817452
But i want a collection that holds MyClass<T> objects which could be strings, ints or any other type all in one collection.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22818007
Than this collection would not be strongly typed, as you want it to be, since it contains different types (of(int), of(string).
0
 
LVL 23

Expert Comment

by:Tiggerito
ID: 22819281
An example of the problem would be.

Say you have your generic class

class MyClass<T>
{
  public T Value() {...}
}

and create two subclasses from it

class MyIntClass: MyClass<int> ...

class MyStringClass: MyClass<string> ...

If you managed to make a generic collection that accepted both these templates. How would you iterate through the collection and use it. You can't do this

foreach(MyClass<T> genericClass in MyCollection)
{
  T v = genericClass.Value; // what type does this return, int, string?????
}

0
 
LVL 23

Expert Comment

by:Tiggerito
ID: 22819302
I was looking for a god description of the limitations that generics has. I didn't find one but I found this related thread...

http://bytes.com/forum/thread446369.html
0
 

Author Comment

by:eddyevations
ID: 22820882
Yes Tiggerito, that is exactly what i would want to do and maybe use a switch statement to process  the generic value.
0
 
LVL 23

Expert Comment

by:Tiggerito
ID: 22821744
You can't even switch on normal types, let alone generic!

Your problem is a bit like saying, "I want a collection that contains ints and string but is not object based".

Generics may add a wrapper around it, but your still tring to make a collection of incompatible types.

The only other possible solution (not tested) is if you enforce all your generic classes to be based on a specific class...

class MyGenericClass<T> where T : MyBaseClass

You may then be able to create a collection of

class MyCollection : List<MyGenericClass<MyBaseClass>>
0
 
LVL 18

Accepted Solution

by:
Priest04 earned 500 total points
ID: 22824113
Having atrongly typed collection of MyClass<> is NOT possible, I have received a confirmation about this. Basically because MyClass<> is more of a template than a class itself. The only way to go with it is having a weekly typed collection, and do some casting later.
0
 

Author Closing Comment

by:eddyevations
ID: 31509780
Thanks guys.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now