Generics return type cannot cast.
-
I cannot get generics to return a type that it does not know is correct. I have a collection of general objects each with a sorted keyfield ID cTypeA 2 cTypeB 3 cTypeA 7 cTypeA 9 cTypeB 12 in one ArrayList, all of which derive from cType. I want to create a separate list of ID references using generics, but which returns the actual objects in an indexer. That way I am assured of type safety when using the object (displaying in a listview say) rather than casting at the end use point:
public class cList<T>
{
public cList(cIDList UseOwner)
{
Owner = UseOwner;
ListItems = new ArrayList();
}public T this\[int Index\] { get { if ((Index >= 0) && (Index < ListItems.Count)) { return (T) Owner.GetObject((int)ListItems\[Index\]); //\*\*\*problem cannot implicitely convert to type T } } } private cIDList Owner; private ArrayList ListItems; //would contain 2,7,9
}
to implement this
cList<cTypeA> AItem = new cList<cTypeA>(MyIDList);
(assume items added)
cTypeA UseA = AItem[2];Is there a way to solve this problem? The only way I can see is to do the above without generics, copy and paste then change the return cast type for what I use now, and in the future. Not easily manageable! Thanks.
-
I cannot get generics to return a type that it does not know is correct. I have a collection of general objects each with a sorted keyfield ID cTypeA 2 cTypeB 3 cTypeA 7 cTypeA 9 cTypeB 12 in one ArrayList, all of which derive from cType. I want to create a separate list of ID references using generics, but which returns the actual objects in an indexer. That way I am assured of type safety when using the object (displaying in a listview say) rather than casting at the end use point:
public class cList<T>
{
public cList(cIDList UseOwner)
{
Owner = UseOwner;
ListItems = new ArrayList();
}public T this\[int Index\] { get { if ((Index >= 0) && (Index < ListItems.Count)) { return (T) Owner.GetObject((int)ListItems\[Index\]); //\*\*\*problem cannot implicitely convert to type T } } } private cIDList Owner; private ArrayList ListItems; //would contain 2,7,9
}
to implement this
cList<cTypeA> AItem = new cList<cTypeA>(MyIDList);
(assume items added)
cTypeA UseA = AItem[2];Is there a way to solve this problem? The only way I can see is to do the above without generics, copy and paste then change the return cast type for what I use now, and in the future. Not easily manageable! Thanks.
You can use a generic
List
instead of an untypedArrayList
, then copy the required references in the c'tor. (This assumes you are using .NET 3.5 - and of course yourcIDList
must be anIEnumerable
, i.e. derived from any of the usual .NET collection classes). Here's what it will look like:public class cList<T>
{
private readonly List<T> ListItems = new List<T>();public cList(cIDList UseOwner) { if (UseOwner != null) { ListItems.AddRange(UseOwner.OfType<T>()); } } public T this\[int Index\] { get { if ((Index >= 0) && (Index < ListItems.Count)) { return ListItems\[Index\]; } return null; // or default(T) or even better: throw an ArgumentOutOfRangeException... } }
}
Regards Thomas
www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.modified on Sunday, May 3, 2009 11:35 AM
-
I cannot get generics to return a type that it does not know is correct. I have a collection of general objects each with a sorted keyfield ID cTypeA 2 cTypeB 3 cTypeA 7 cTypeA 9 cTypeB 12 in one ArrayList, all of which derive from cType. I want to create a separate list of ID references using generics, but which returns the actual objects in an indexer. That way I am assured of type safety when using the object (displaying in a listview say) rather than casting at the end use point:
public class cList<T>
{
public cList(cIDList UseOwner)
{
Owner = UseOwner;
ListItems = new ArrayList();
}public T this\[int Index\] { get { if ((Index >= 0) && (Index < ListItems.Count)) { return (T) Owner.GetObject((int)ListItems\[Index\]); //\*\*\*problem cannot implicitely convert to type T } } } private cIDList Owner; private ArrayList ListItems; //would contain 2,7,9
}
to implement this
cList<cTypeA> AItem = new cList<cTypeA>(MyIDList);
(assume items added)
cTypeA UseA = AItem[2];Is there a way to solve this problem? The only way I can see is to do the above without generics, copy and paste then change the return cast type for what I use now, and in the future. Not easily manageable! Thanks.
You probably need to redesign whatever it is you're trying to do. Otherwise, try
cTypeA UseA = AItem[2] as cTypeA ;