By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,906 Members | 1,771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,906 IT Pros & Developers. It's quick & easy.

Constructor in sublass of List<>

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
"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

P: n/a

"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

P: n/a
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

P: n/a
"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

P: n/a

"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.