Referencing a Generic

P

PJ

I have a class defined as so:

public class Pager : Control
{
private PagingList<T> itemList;
....

I'm getting an error message that says, "The type or namespace name 'T'
could not be found. Obviously, I'm just trying to define a class that
PagingList is a class that compiles just fine that inherits from List<T>.

What gives? thx,

~PJ
 
P

PJ

ok, so apparently i can do:

private PagingList<object> itemList;

which works for my implementation, as I am only concerned w/ the methods of
the subclass PagingList<T> which inherits from List<T>, but I still do not
understand why I cannot use a generic in this situation?

~PJ
 
J

Jon Skeet [C# MVP]

PJ said:
I have a class defined as so:

public class Pager : Control
{
private PagingList<T> itemList;
...

I'm getting an error message that says, "The type or namespace name 'T'
could not be found. Obviously, I'm just trying to define a class that
PagingList is a class that compiles just fine that inherits from List<T>.

What gives? thx,

You need to make your Pager a generic class as well - otherwise it
doeesn't know what T is:

public class Pager<T> : Control
{
....
}
 
M

Marc Gravell

I think all that you are missing is a <T> on the Pager - i.e. if this was
public class Pager<T> : Control you would have a type-safe pager, and
itemList would automatically adopt the same T as the T used in Pager. Note,
however, that generic controls don't work very well in the designer; you
will probably need to add them to forms programatically.

The whole point of generics is to provide a re-usable, type-safe construct.

In your example, what objects would the PagingList contain? i.e. what would
it's Add method look like? If you want it accept any type of object, then
that would (as you have found) by PagingList<object>, and it will be
Add(object obj) - however, the main point of generics is that I might want a
strongly typed list of MachinePart objects - i.e. it will accept only
MachinePart objects and subclasses - this would be PagingList<MachinePart>,
and would have Add(MachinePart obj) and the indexer would be "MachinePart
this[int index]" etc.

Does that help?
 
?

=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

PJ said:
I have a class defined as so:

public class Pager : Control
{
private PagingList<T> itemList;
....

I'm getting an error message that says, "The type or namespace name 'T'
could not be found. Obviously, I'm just trying to define a class that
PagingList is a class that compiles just fine that inherits from List<T>.

What gives? thx,

~PJ

It is impossible to define a class that inherits from a generic class
like that. In your example I would say it would be impossible because
the code would need to know what kind of type it is actually dealing with.

However, I've also seen needs for defining a method that takes a List<T>
argument and not be generic itself, and this is also impossible.

In other words, if you want the above code to compile, make Pager a
generic class as well, otherwise you're going to need to find a
different solution. For instance, you could use IList or similar without
using it as a generic interface.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Constructor in sublass of List<> 5
Polymorphic generics 4
Inherit from generic class type 2
Why won’t my generic method compile? 1
Generics; OO 4
where T : class 1
Generic puzzle 4
Generics help 6

Top