?
Solved

How can i create  a Generic collection of Generic type classes

Posted on 2008-10-24
22
Medium Priority
?
751 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

719 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