Reputation: 111
I am trying to develop a routine that will run an sql statement and use the results to populate class objects that are derived from an abstract class, something like the following:
public static List<T> PopulateList<T>(string sql)
{
List<T> list = new List<T>();
// get sql data
foreach (row in resultset)
{
list.Add(new T(row));
}
return list;
}
The constructor of the abstract class (i.e., "T") calls an abstract/virtual method which, of course, would be overidden and potentially different for each derived class.
The problem is that C# won't allow an abstract class object to be created and populated even though it represents and will be used for a derived class object.
How can I accomplish this?
I have tried to design the routine but haven't figured out a way to get it to work.
Upvotes: 0
Views: 71
Reputation: 76943
The problem is that C# won't allow an abstract class object to be created and populated even though it represents and will be used for a derived class object.
Yes indeed. So you have MyAbstractClass
with some abstract
methods. You can implement a class
that extends your MyAbstractClass
and implements the abstract
methods in some dummy manner and use this MyDerivedClass
instead.
Upvotes: 0