findall question

T

tshad

I have 2 classes (one an generic list of the 1st class)
********************************************************
namespace rez
{
public class JobBucket
{
public string Job { get; set; }
public int Bucket { get; set; }

public JobBucket()
{
Job = "";
Bucket = 0;
}

public JobBucket(string job, int bucket)
{
this.Job = job;
this.Bucket = bucket;
}

public override string ToString()
{
return Job + " / " + Bucket;
}
}
public class JobBucketList : List<JobBucket> { }
*******************************************

Why can I do this:

List<JobBucket> jb =
(List<JobBucket>)jobs.FindAll(delegate(JobBucket jb1)
{
return jb1.Job == "14975-9";
});

But not:

JobBucketList jb =
(JobBucketList)jobs.FindAll(delegate(JobBucket jb1)
{
return jb1.Job == "14975-9";
});


Aren't they the same?

It builds fine but dies when it executes the statement.

Thanks,

Tom
 
P

Peter Duniho

tshad said:
[...]
Why can I do this:

List<JobBucket> jb =
(List<JobBucket>)jobs.FindAll(delegate(JobBucket jb1)
{
return jb1.Job == "14975-9";
});

But not:

JobBucketList jb =
(JobBucketList)jobs.FindAll(delegate(JobBucket jb1)
{
return jb1.Job == "14975-9";
});


Aren't they the same?

No, they are not the same. In your first example, the destination
variable has the type List<JobBucket>, which is the same type returned
by the FindAll() method. In your second example, the destination
variable has the type JobBucketList, which is _not_ the same type
returned by the FindAll() method.

Just as in the first example, FindAll() returns an instance of
List<JobBucket>, so too in the second example does it return an instance
of List<JobBucket>. Specifically, the class List<JobBucket> is _not_ a
JobBucketList. And you can't just go around casting instances to types
that they aren't actually instances of.

There are a variety of ways around this, but honestly the best approach
IMHO is to get rid of the JobBucketList class and just use the
List<JobBucket> type directly. There are a variety of problems that
come up when you try to inherit a collection class like that, this being
just one of them.

The basic problem is that most people who want to write an inheritance
like this don't have a good intuitive sense of what the inheritance will
actually allow and what it won't, and so they often get confused about
the behaviors they observe. And being confused by your own code that
you wrote is generally a bad idea. :) Keep it simple.

Pete
 
T

tshad

Peter Duniho said:
tshad said:
[...]
Why can I do this:

List<JobBucket> jb =
(List<JobBucket>)jobs.FindAll(delegate(JobBucket jb1)
{
return jb1.Job == "14975-9";
});

But not:

JobBucketList jb =
(JobBucketList)jobs.FindAll(delegate(JobBucket jb1)
{
return jb1.Job == "14975-9";
});


Aren't they the same?

No, they are not the same. In your first example, the destination
variable has the type List<JobBucket>, which is the same type returned by
the FindAll() method. In your second example, the destination variable
has the type JobBucketList, which is _not_ the same type returned by the
FindAll() method.

Just as in the first example, FindAll() returns an instance of
List<JobBucket>, so too in the second example does it return an instance
of List<JobBucket>. Specifically, the class List<JobBucket> is _not_ a
JobBucketList. And you can't just go around casting instances to types
that they aren't actually instances of.

There are a variety of ways around this, but honestly the best approach
IMHO is to get rid of the JobBucketList class and just use the
List<JobBucket> type directly. There are a variety of problems that come
up when you try to inherit a collection class like that, this being just
one of them.

The basic problem is that most people who want to write an inheritance
like this don't have a good intuitive sense of what the inheritance will
actually allow and what it won't, and so they often get confused about the
behaviors they observe. And being confused by your own code that you
wrote is generally a bad idea. :) Keep it simple.
Makes sense.

Thanks,

Tom
 

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