Consider the following classes:
class TypeA;
class TypeB : TypeA;
class TypeC : TypeA;
class TypeD : TypeA;
and the following List<> types:
List<TypeB> listTypeB;
List<TypeC> listTypeC;
List<TypeD> listTypeD;
Now TypeA has a property Prop1 of type Object1 and I want to locate which list has stored within it an item with Prop1 of a given value. Is there a way in which I can do something like the following, so that I only need to write the search code once?
bool LocateInAnyList(Object1 findObj)
{
bool found = false;
found = ContainsProp1(findObj, listTypeB);
if(!found)
{
found = ContainsProp1(findObj, listTypeC);
}
if(!found)
{
found = ContainsProp1(findObj, listTypeD);
}
return found;
}
bool ContainsProp1(Object1 searchFor, List<TypeA> listToSearch)
{
bool found = false;
for(int i = 0; (i < listToSearch.Count) & !found; i++)
{
found = listToSearch[i].Prop1 == searchFor;
}
return found;
}