Constructor in sublass of List<>

P

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
 
J

Joanna Carter [TeamB]

"PJ" <[email protected]> a écrit dans le message de (e-mail address removed)...

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

David Browne

PJ said:
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
 
P

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!
 
J

Joanna Carter [TeamB]

"PJ" <[email protected]> a écrit dans le message de (e-mail address removed)...

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

Nick Hounsome

PJ said:
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.
 

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

Top