B
Berryl Hesh
I have a list of base class items and I need an operation to replace an item
that matches some criteria. The problem I'm having is that the state
information I need to use FindIndex or something similar is in a derived
class. How would you go about doing that?
for the sake of discussion, here's some toy code.
class Foo
{
public int X;
public Foo(int x) { X = x; }
}
class Bar : Foo
{
int Y;
public Bar(int x, int y) : base(x) { Y = y; }
}
[Test]
public void TestCase() {
var list = new List<Foo> {new Foo(10), new Foo(8), new Bar(2, 7)};
var foundElement = list.Find(x => x.X == 10);
Assert.That(foundElement.X, Is.EqualTo(10));
// find element that is a Bar with Y = 7
}
Thanks!
that matches some criteria. The problem I'm having is that the state
information I need to use FindIndex or something similar is in a derived
class. How would you go about doing that?
for the sake of discussion, here's some toy code.
class Foo
{
public int X;
public Foo(int x) { X = x; }
}
class Bar : Foo
{
int Y;
public Bar(int x, int y) : base(x) { Y = y; }
}
[Test]
public void TestCase() {
var list = new List<Foo> {new Foo(10), new Foo(8), new Bar(2, 7)};
var foundElement = list.Find(x => x.X == 10);
Assert.That(foundElement.X, Is.EqualTo(10));
// find element that is a Bar with Y = 7
}
Thanks!