D
David Longnecker
I'm working to create a base framework for our organization for web and client-side
applications. The framework interfaces with several of our systems and provides
the business and data layer connectivity for basic operations and such.
I've ran into a snag that I just can't think myself out of.
Here's an example:
I have an object for a Student called StudentRecord. It has properties such
as name, grade, identification number, etc.
public class StudentRecord : IComparable
{
private int _studentId;
public int StudentId { get { return _studentId; } set { _studentId = value;
} }
public override string ToString() { get { return string.Format("{0} {1}",
_firstName, _lastName); }
... etc.
}
From there, I have a "roster" of students, thus a collection of those StudentRecord
objects. I would like to inherit from List<StudentRecord> to provide Find,
FindAll, Sort, and other functionality to the consumer of the API. (1)*
public class StudentRoster : List<StudentRecord>
{
// I'm not providing any additional functionality here at this time,
// simply providing the easier call rather than requring them to know List<object>.
}
Now, roster.Find works just fine because, I'm assuming, the list is based
off StudentRecord.
StudentRecord studentA = roster.Find(delegate(StudentRecord x) { return x.LastName.Equals("Longnecker");
});
This returns me a populated student object based off a prior created collection
of student Objects.
I run into an issue with:
StudentRoster studentsInGrade8 = roster.FindAll(delegate (StudentRecord x)
{ return x.Grade.Equals("08"); } );
FindAll apparently returns a raw list of List<StudentRecord> rather than
StudentRoster (since I haven't specified it). I've attempted to modify my
StudentRoster class to add:
public StudentRoster FindAll(Predicate<StudentRecord> item)
{
StudentRoster items = (StudentRoster) Items;
return items.FindAll(match);
}
However, that fails with the same error--it cannot convert List<StudentRecord>
to StudentRoster. I've attempted to cast it explicitly (using 'as StudentRoster'
and (StudentRoster)) to no avail.
Google hasn't turned up anything thus far and neither has MSDN (though I'm
assuming, perhaps, I'm not searching for the magical word)... What would
be the best way to create "collections of objects" that can be searched through
using Find at the public API level?
(1)* == Note that CLS does specify not to expose List<T>; however, Collection<T>
doesn't have Find, Sort, FindAll, etc. Is there a better option?
Any reference material, etc. would be greatly appreciated!
Thanks in advance.
-dl
applications. The framework interfaces with several of our systems and provides
the business and data layer connectivity for basic operations and such.
I've ran into a snag that I just can't think myself out of.
Here's an example:
I have an object for a Student called StudentRecord. It has properties such
as name, grade, identification number, etc.
public class StudentRecord : IComparable
{
private int _studentId;
public int StudentId { get { return _studentId; } set { _studentId = value;
} }
public override string ToString() { get { return string.Format("{0} {1}",
_firstName, _lastName); }
... etc.
}
From there, I have a "roster" of students, thus a collection of those StudentRecord
objects. I would like to inherit from List<StudentRecord> to provide Find,
FindAll, Sort, and other functionality to the consumer of the API. (1)*
public class StudentRoster : List<StudentRecord>
{
// I'm not providing any additional functionality here at this time,
// simply providing the easier call rather than requring them to know List<object>.
}
Now, roster.Find works just fine because, I'm assuming, the list is based
off StudentRecord.
StudentRecord studentA = roster.Find(delegate(StudentRecord x) { return x.LastName.Equals("Longnecker");
});
This returns me a populated student object based off a prior created collection
of student Objects.
I run into an issue with:
StudentRoster studentsInGrade8 = roster.FindAll(delegate (StudentRecord x)
{ return x.Grade.Equals("08"); } );
FindAll apparently returns a raw list of List<StudentRecord> rather than
StudentRoster (since I haven't specified it). I've attempted to modify my
StudentRoster class to add:
public StudentRoster FindAll(Predicate<StudentRecord> item)
{
StudentRoster items = (StudentRoster) Items;
return items.FindAll(match);
}
However, that fails with the same error--it cannot convert List<StudentRecord>
to StudentRoster. I've attempted to cast it explicitly (using 'as StudentRoster'
and (StudentRoster)) to no avail.
Google hasn't turned up anything thus far and neither has MSDN (though I'm
assuming, perhaps, I'm not searching for the magical word)... What would
be the best way to create "collections of objects" that can be searched through
using Find at the public API level?
(1)* == Note that CLS does specify not to expose List<T>; however, Collection<T>
doesn't have Find, Sort, FindAll, etc. Is there a better option?
Any reference material, etc. would be greatly appreciated!
Thanks in advance.
-dl