473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1899
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
4
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>....
2
by: Brian Pelton | last post by:
I am not sure how to fix this problem I've stumbled into... I have a list<> of an interface type. I need to pass that list to a method that adds more objects to the list. But, eventually, I...
3
by: Varangian | last post by:
Hello, there I have a problem with regards to System.Collections.Generic.List<T> I need to pass a class with implements an interface - TestClass : IPerson I put this class in a...
9
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private...
0
by: Iron Moped | last post by:
I'm airing frustration here, but why does LinkedList<not support the same sort and search methods as List<>? I want a container that does not support random access, allows forward and reverse...
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
56
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be...
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.