Solved

How can i create  a Generic collection of Generic type classes

Posted on 2008-10-24
22
749 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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:Tony McCreath
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
 

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:Tony McCreath
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:Tony McCreath
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:Tony McCreath
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
"The resource cannot be found" 1 43
C# Get Current Exchange Rate from XE 9 42
Load XML element 3 43
XML Constructor Throwing Incorrectly Structured Document Error 4 23
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…

734 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