NOTE: .NET Pad code HERE.
I have a base class that takes a generic type parameter.
Each class it will form the base of has a corresponding Collection Class which consists of only this:
public class List_SomeType : List<SomeTime> {
public List_SomeType() {
}
}
That’s it: It only a List<T> container.
However, it is posing a problem in my base class because one of the methods is to return this collection.
I tried creating the following Get method, which made perfect sense to me:
public TList Get() {
var list = new TList();
using (var cmd = new SqlCommand(SP_GET, m_openConn)) {
cmd.CommandType = CommandType.StoredProcedure;
using (var r = cmd.ExecuteReader()) {
while (r.Read()) {
list.Add(FillDataRecord(r));
}
}
cmd.Connection.Close();
}
return list;
}
public class TList : List<T> {
public TList() { }
}
In my derived class, I planned to call this base class as follows:
public class BuyerDB : DAL_Base<Buyer> {
private static BuyerDB one;
static BuyerDB() {
one = new BuyerDB();
}
public static BuyerList GetBuyerList() {
return (BuyerList)one.Get(); // <= ERROR HERE!
}
}
The ERROR HERE states:
Cannot convert type ‘DAL_Base.TList’ to ‘BuyerList’
How do I convert back to a collection of my generic types?
EDIT:
Mark wants to know what BuyerList is.
I thought that was understood from what I wrote.
Apparently not, so here it is:
public class BuyerList : List<Buyer> { }
That’s all!
This compiles:
I am not sure why the other list would not work, but I’d guess it has something to do with checks done inside of the C# language.