How can i create a Generic collection of Generic type classes

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?
eddyevationsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Priest04Commented:
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
eddyevationsAuthor Commented:
Sorry, I do not follow..."QueryStringValues" is my own generic class.
0
eddyevationsAuthor Commented:
I should have been more clear...i apologize.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Priest04Commented:
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
eddyevationsAuthor Commented:
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
eddyevationsAuthor Commented:
btw...Thanks for responding Priest04
0
Priest04Commented:
You are welcome.


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

Open in new window

0
eddyevationsAuthor Commented:
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
Tony McCreathTechnical SEO ConsultantCommented:
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
eddyevationsAuthor Commented:
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
Priest04Commented:
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
eddyevationsAuthor Commented:
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
Priest04Commented:
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
Priest04Commented:
I have only given an example of string, you will replace it with the corresponding type.
0
eddyevationsAuthor Commented:
But i want a collection that holds MyClass<T> objects which could be strings, ints or any other type all in one collection.
0
Priest04Commented:
Than this collection would not be strongly typed, as you want it to be, since it contains different types (of(int), of(string).
0
Tony McCreathTechnical SEO ConsultantCommented:
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
Tony McCreathTechnical SEO ConsultantCommented:
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
eddyevationsAuthor Commented:
Yes Tiggerito, that is exactly what i would want to do and maybe use a switch statement to process  the generic value.
0
Tony McCreathTechnical SEO ConsultantCommented:
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
Priest04Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
eddyevationsAuthor Commented:
Thanks guys.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.