Click here to Skip to main content
15,942,292 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

C#
Interface IFurnitureDao{...}

Class TableDao : IFurnitureDao {...}
Class ChairDao : IFurnitureDao {...}
Class AlmirahDao : IFurnitureDao {...}


I have a generic class of type IFurnitureDao...

C#
Class MyGenericClass<T> where T:IFurnitureDao {...}


The objects of MyGenericClass is stored in an ArrayList as shown below :

C#
ArrayList myList = new ArrayList {new MyGenericClass<TableDao>{..} , new MyGenericClass<ChairDao>{..}, MyGenericClass<AlmirahDao>{..} }

While retriving the data from ArrayList I am tryign to cast teh objects into IFurnitureDao.

C#
List<MyGeneRicClass<IFurnitureDao>> newList = myList.OfType<MyGenericClass<IFurnitureDao>>().toList();

I am receiving an error stating
unable to cast objects from MyGenericClass<TableDao> to MyGenericClass<IFurnitureDao>. 


Can anyone guide me on this . Thanks !

Regards,
Divya
Posted
Updated 26-Jun-13 4:46am
v2

You can't cast concrete objects to interfaces. Your concrete objects implement the interface but may also implement other interfaces or contain additional properties/methods. Therefore you can't go backwards.

Plus, an interface is NOT an object. It is only a description (or blueprint) for an object which will be applied to an object that implements that interface.

If you want to cast your specific objects back to a generic object, you will need to create a generic class which implements nothing but your interface then assign each property as you iterate the list of concrete objects. Or, create a base class from your interface then derive your concrete objects from the base class. Like this:

C#
Interface IFurnitureDao{...}

Class BaseDao : IFurnitureDao {...}
Class TableDao : BaseDao {...}
Class ChairDao : BaseDao {...}
Class AlmirahDao : BaseDao {...}


If you do this, then you can cast backwards as long as there are no real differences:
C#
Class MyGenericClass<t> where T:BaseDao {...}</t>
 
Share this answer
 
The classes you are trying to case are not generic. You already instantiated generics into a complete classes.

Say, MyGenericClass<> is generic, MyGenericClass<TableDao> (if TableDao is a complete class, not generic parameter) is not.

If they were generic classes, the whole idea of casting would not make any sense: there are not instances of generic classes.
Your classes are non-generic, but you cannot cast them because they are not assignment-compatible. It's not clear why are you trying to assign instances of them.

For some background, please see this: http://msdn.microsoft.com/en-us/library/ee207183.aspx[^].

But, to understand it, you should get better understanding of more basic programming topics: classes and instances, generic classes, complete types, instantiation of genetics, and other generic topics.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900