471,315 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Constructor in sublass of List<>

PJ
I have a class definition :

public class PagingList<T> : List<T>
{
private int pageSize, pageNumber;
public PagingList()
{
pageSize = (this.Count == 0) ? 1 : this.Count;
pageNumber = 1;
}
My problem is that this.Count is 0, apparently the list items aren't
available in the constructor. Is it possible to get access to the list
items in the constructor?

~PJ

Jan 14 '06 #1
5 1809
"PJ" <pj***@hotmail.com> a écrit dans le message de news:
vd********************@speakeasy.net...

|I have a class definition :
|
| public class PagingList<T> : List<T>
| {
| private int pageSize, pageNumber;
| public PagingList()
| {
| pageSize = (this.Count == 0) ? 1 : this.Count;
| pageNumber = 1;
| }
|
|
| My problem is that this.Count is 0, apparently the list items aren't
| available in the constructor. Is it possible to get access to the list
| items in the constructor?

Of course the items are available, but how can there be more than 0 items in
the list if you have not yet finished creating it ? Until the constructor is
complete, you cannot add items externally but you could add items inside the
constructor if that suits your purpose.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 14 '06 #2

"PJ" <pj***@hotmail.com> wrote in message
news:vd********************@speakeasy.net...
I have a class definition :

public class PagingList<T> : List<T>
{
private int pageSize, pageNumber;
public PagingList()
{
pageSize = (this.Count == 0) ? 1 : this.Count;
pageNumber = 1;
}
My problem is that this.Count is 0, apparently the list items aren't
available in the constructor. Is it possible to get access to the list
items in the constructor?


No the object isn't constructed yet so it can't have had items added to it
already.

But you can add a parameter to the constructor.
EG:

public class PagingList<T> : List<T>
{
private int pageSize, pageNumber;
public PagingList(int pageSize)
{
this.pageSize = pageSize;
this.pageNumber = 1;
}

David
Jan 14 '06 #3
PJ
ok, i wasn't thinking, it's not being contructed w/ the collection of items.
There is a constructor for that and, of course, the argument would be
available there...

thx!

"Joanna Carter [TeamB]" <jo****@not.for.spam> wrote in message
news:eS**************@TK2MSFTNGP10.phx.gbl...
"PJ" <pj***@hotmail.com> a écrit dans le message de news:
vd********************@speakeasy.net...

|I have a class definition :
|
| public class PagingList<T> : List<T>
| {
| private int pageSize, pageNumber;
| public PagingList()
| {
| pageSize = (this.Count == 0) ? 1 : this.Count;
| pageNumber = 1;
| }
|
|
| My problem is that this.Count is 0, apparently the list items aren't
| available in the constructor. Is it possible to get access to the list
| items in the constructor?

Of course the items are available, but how can there be more than 0 items
in
the list if you have not yet finished creating it ? Until the constructor
is
complete, you cannot add items externally but you could add items inside
the
constructor if that suits your purpose.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Jan 14 '06 #4
"PJ" <pj***@hotmail.com> a écrit dans le message de news:
ta********************@speakeasy.net...

| ok, i wasn't thinking, it's not being contructed w/ the collection of
items.
| There is a constructor for that and, of course, the argument would be
| available there...

I'm not sure what you mean here; there is no other list, this is the
constructor of *the* list. You appear to be deriving from a generic list to
create a paged generic list. When you instantiate this derived paged list
class, this constructor will be run :

public class PagingList<T> : List<T>
{
private int pageSize, pageNumber;
public PagingList()
{
pageSize = 10; // for example
pageNumber = 1;
}

The above code is all that it is possible to know as the list count has to
be 0 at this stage.

Your code implied that the pageSize would want to be the same as the count;
this is unusual as paged lists normally have a page size that is some number
which is a factor of the count, e.g. for 1000 objecs, the pageSize could be
10 or 100.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 14 '06 #5

"PJ" <pj***@hotmail.com> wrote in message
news:vd********************@speakeasy.net...
I have a class definition :

public class PagingList<T> : List<T>
{
private int pageSize, pageNumber;
public PagingList()
{
pageSize = (this.Count == 0) ? 1 : this.Count;
pageNumber = 1;
}
My problem is that this.Count is 0, apparently the list items aren't
available in the constructor. Is it possible to get access to the list
items in the constructor?

~PJ


Re: 0 - others have already pointed out the problem.

IMHO and without knowing exactly what you are trying to acheive it is highly
unlikely that you should be deriving from List<T> anyway since you cannot
affect the behaviour of the object when it is treated as a List<T>,
IList<T>, ICollection<T> or IEnumerable<T>

I suspect that what you should really be doing is implementing IList<T> and
possibly using a private List<T> in the implementation.
Jan 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Dave | last post: by
2 posts views Thread by Brian Pelton | last post: by
reply views Thread by Iron Moped | last post: by
7 posts views Thread by Andrew Robinson | last post: by
35 posts views Thread by Lee Crabtree | last post: by
reply views Thread by rosydwin | 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.