Hi,
I have this code and i need to be able to search through different lists of HierarchyItem
s and return a list of the correct type
i.e. Hierarchy h = new Hierarchy();
//add a load of items of HierarchyItemA type;
List<HierarchyItemA> results = h.Search("text");
CODE:
public class Hierarchy
{
private List<HierarchyItem> items;
public Hierarchy()
{
items = new List<T>();
}
public void AddItem(HierarchyItem item)
{
items.Add(item);
}
public List<T> Search(string searchText)
{
List<T> results = new List<T>();
foreach (HierarchyItem item in items)
{
if (item.DisplayText().ToLower().Contains(searchText.ToLower()))
{
results.Add(item);
}
}
return results;
}
}
public abstract class HierarchyItem
{
public string DisplayText()
{
//returns a string
}
}
public class HierarchyItemA : HierarchyItem
{
//do whatever
}
public class HierarchyItemB : HierarchyItem
{
//do whatever
}
Cheers
EDIT: there are several hierarchies each one only has one type in it. the Hierarchy.Search(text) should return a list containing items of the correct type (correct being of type A or B)