Hi all,
I am just playing around with some ideas and was wondering if the following would be possible without too much hassle...
Imagine a class like this:
public class Animals : ObservableCollection<Animal>
{
}
And of course this radically rare class
public class Animal
{
public string Name{ get; set; }
public bool IsAlive{ get; set; }
public bool Buried{ get; set; }
}
Now if someone uses the
Animals
class he/she would propably not be interested in retrieving animals that are not alive and buried (if so I could provide a function to get these if neccessary).
So would it somehow be possible to modify the
Animals
class so that it only respects instances of
Animal
that are actually alive and not buried?
So after an instance of
Animal
is actually buried (and of course no longer alive - which might automatically change if buried alive) it should not be available anymore by using the standard retrieval methods of the
Animals
class (also Count and Linq related stuff like First, Where, etc. - you get the picture).
So far I implemented the
INotifyPropertyChanged
interface in the
Animal
class to allow
Animals
to remove those instances (dead and buried) and store them internally, which works fine. But would there be a more elegant solution to the problem?
UPDATE:
Maybe I can simplify it a little bit more.
Would it be possible to modify the
Animals
class somehow to only return instances of
Animal
where not (Alive = false AND Buried = true) so that they can no longer be seen by the outside world but to keep it in the list?
Mabe by overriding/overwriting the enumerator like this:
new public IEnumerator GetEnumerator()
{
IEnumerator ie = base.GetEnumerator();
while (ie.MoveNext())
{
Animal animal = (Animal)ie.Current;
if(animal.Alive && !animal.Buried)
yield return ie.Current;
}
}
..but would that also affect Count, First, etc of the List?
UPDATE:
I figured out that the above will affect foreach (which was ecpectable) but not Count or Linq methods...
I have hard time expressing myself in english here, please be mercyfull :)
Any thoughts are kindly appreciated,
have a great day
Andy