Try:
public Dictionary<T, bool> DeleteSheets<T>(List<T> data)
{
Dictionary<T, bool> results = new Dictionary<T, bool>();
foreach (T s in data)
results.Add(s, Delete<T>(s));
return results;
}
public bool Delete<T>(T s) { return true; }
Griff,
The Delete method executes different code, based on what function it is, but they all return bools. How can I handle that? Use a delegate for this?
That's not as simple as it sounds: you can use a Delegate:
Dictionary<string, bool> data = DeleteSheets(new List<string>() { "hello", "goodbye" }, DeleteString);
Dictionary<int, bool> data2 = DeleteSheets(new List<int>() { 1, 2, 3 }, DeleteString);
}
bool DeleteString(string s) { Console.WriteLine(s); return true; }
bool DeleteString(int s) { Console.WriteLine("Del" + s ); return true; }
public delegate bool DeleteMethod<T>(T t);
public Dictionary<T, bool> DeleteSheets<T>(List<T> data, DeleteMethod<T> delete )
{
Dictionary<T, bool> results = new Dictionary<T, bool>();
foreach (T s in data)
results.Add(s, delete(s));
return results;
}
And it will call the appropriate method.
But I'd rather restrict the generic to a base class and do it via overloading instead:
Dictionary<Base, bool> data2 = DeleteSheets(new List<Base>() { new A(), new A(), new B() });
}
public Dictionary<T, bool> DeleteSheets<T>(List<T> data ) where T: Base
{
Dictionary<T, bool> results = new Dictionary<T, bool>();
foreach (T s in data)
results.Add(s, s.Delete());
return results;
}
public abstract class Base
{
public abstract bool Delete();
}
public class A : Base
{
public override bool Delete()
{
Console.WriteLine("A Delete");
return true;
}
}
public class B : Base
{
public override bool Delete()
{
Console.WriteLine("B Delete");
return true;
}
}
It just seems like a "cleaner" solution.