Solved

How can i create  a Generic collection of Generic type classes

Posted on 2008-10-24
22
748 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 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