.NET Framework - findall question

Asked By tshad
20-Jan-10 02:29 PM
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
FindAll
(1)
JobBucketList
(1)
Bucket
(1)
JobBucket
(1)
Inheritance
(1)
Dies
(1)
Int
(1)
Jb1
(1)
  Peter Duniho replied to tshad
20-Jan-10 03:14 PM
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 cannot just go around casting instances to types
that they are not 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 do not have a good intuitive sense of what the inheritance will
actually allow and what it will not, 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
  tshad replied to Peter Duniho
20-Jan-10 03:26 PM
Makes sense.

Thanks,

Tom
Create New Account
help
type. Not every Form has a "Label30". The big difference is that we now have inheritance and (explicit) interfaces. This is the VB.Net solution to your problem: If all your forms have a "Label30", define a common base Form and make use of Form inheritance. This makes all inherited Forms look like the base form, and you can add additional construction zone. In the long run, and for new projects anyway, you should choose the inheritance solution if possible. So, the Interface solution is: Interface IForm Property Width() As Integer Property new one and do the right design steps, it is easier than before, thanks to inheritance. - - Armin You are trying to be helpful but you do not read half of my did not need that. use ld s: I am not familiar with this interface and inheritance concepts. Looks like I will have to read up a bit. nting e Form th is early binding in this case. Your focus is more on designing types, which includes inheritance and interface implementation. With these features, everything can be solved without late binding. Another step as obvious or even used that often. For instance, the concept of interfaces and interface inheritance - it is just that vb6 did not have explicit interfaces. VB.NET just takes VB6 moves them to the next level - true OOP. Giving not only interface but actual implementation inheritance (with forms this is sometimes called visual inheritance, but it is really just plain old
Finding folders where ACL Inheritance is off .NET Framework I am very new to Powershell. I have found a lot of stuff on using the $acl = get-acl command and $acl.AreAccessRulesProtected to determine if inheritance is on or off. I am trying to write a script that will recurse a directory tree and look at inheritanceon the subfolders. If inheritance is ON ($False) then ignore the subfolder, if inheritance is OFF($True) then output (append), to a csv, the complete path (c: \ Folder \ SubFolder), Owner, & all Users / Groups with Permission Levels. I do not want to change the inheritance, just output the information if it is OFF. Thanks for the Help. Kevin Powershell Discussions the help, this first block of code works well, and outputs a list folders where inheritance is turned off. Now I just need to get the script toalso output the "Owner groups were there. Thank you for all the help. Kevin keywords: Finding, folders, where, ACL, Inheritance, is, off description: I am very new to Powershell. I have found a lot of stuff on using the $acl = get-acl command and $acl.AreAccessRulesProtected to determine if inheritance
inheritance like Animal : List .NET Framework " / > Hi! I know all of the below construction marked 1 node, rather than by the essential nature of a single individual node. So, rather than inheritance, I suspect you rather want to simply OWNS a property returning a LIST, than to C# where one is forced to use interfaces in order to accomplish something like multiple inheritance, and in other languages the line between inherited types and implemented interfaces is much more blurry. Finally, there is the school of thought in OOP that inheritance of implementation (e.g. Java's "extends") should be _strictly_ a private affair, with non do not support that kind of design?though, C++ comes closest with public and private inheritance), and OOP concepts such as "is a" and "has a" should still apply in a context like that, which they could not if you treat inheritance of an interface differently from inheritance of an implementation in terms of object identity. As far as the specific example of OOP concepts was being done, and they refrained from the temptation of a potentially awkward inheritance design, opting rather for a better OOP approach to the classes. I would not say
some code that is finding a car make in a generic collection by using the FindAll method. What I do not understand here is I have two statements marked 1 and Add(car5); list.Add(car6); list.Add(car7); list.Add(car8); / / List<Car> mylist = list.FindAll(delegate(Car c) {return c.Name = = FindCar fc = new FindCar(car8); 1 foreach (Car car in list.FindAll(fc.FindCarPredicate)) { Console.WriteLine(car.Name); } 2 foreach (Car car in list FindAll(new Predicate<Car> (fc.FindCarPredicate))) { Console.WriteLine(car.Name); } } public static void Main() { new Example(); } } / / Tony C# Discussions Class (1) Bool (1) Console (1) FindAll (1) Tony Johansson (1) Main (1) PM (1) Car car (1) The second version was some code that is finding a car make in a generic collection by using the FindAll method. What I do not understand here is I have two
simplified version : public interface IStudentRepository { void Add(Student student); Student FindBy(string ssn); IEnumerable<Student> FindAll(); } Finally there is a service in the model. Again a simplified example : public class StudentService this.studentRepository = studentRepository; } public IList GetStudentsByName(string firstName, string lastName { return (select student from studentRepository.FindAll() where student.FirstName.Equals(firstName) && student.LastName.Equals(lastName) select student).ToList(); } } The service can implementation. The repository Interface can be as simple six methods: Add, Save, Remove, FindBy(id), FindAll and a paged FindAll. Use the query object pattern and add two more FindAll methods. The service can contain DAL functionality not found in the repository implementation. The methods