471,350 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Referencing a Generic

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


Jan 14 '06 #1
5 1198
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

"PJ" <pj***@hotmail.com> wrote in message
news:q4********************@speakeasy.net...
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

Jan 14 '06 #2
PJ <pj***@hotmail.com> wrote:
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
{
....
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 14 '06 #3
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?

"PJ" <pj***@hotmail.com> wrote in message
news:tq******************************@speakeasy.ne t...
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

"PJ" <pj***@hotmail.com> wrote in message
news:q4********************@speakeasy.net...
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


Jan 14 '06 #4
PJ wrote:
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.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Jan 14 '06 #5
Lasse Vågsæther Karlsen wrote:
PJ wrote:
I have a class defined as so:

public class Pager : Control
{
private PagingList<T> itemList;
....
<snip> It is impossible to define a class that inherits from a generic class
Ok, this should say "variable".
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.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Jan 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by student | last post: by
1 post views Thread by jesse.hartwick | last post: by
16 posts views Thread by PromisedOyster | last post: by
9 posts views Thread by Brett Romero | last post: by
4 posts views Thread by Mantorok | last post: by
21 posts views Thread by cmd | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.