Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am getting error while trying to convert one list object to other.
below is my code:

ddlListObj.Add((T)Convert.ChangeType(lstobjUni, typeof(T)));

Even when type of lstobjUni is same as T for particular instance.


Can someone please help.
Posted

you have to implement IConvertable on the object because Covert.ChangeType() expects IConvertable object. So Convert object knows how to convert given object to the type that you need - even it is the same type. you may want to research into Polymorphism in object-oriented programming for a better understanding of why the structure of method require IConvertable.

The main idea is that your object may have multiple forms of interpretation. For example, if you have and Apple object, the apple object can interpreted as a fruit (IFruit), eatable (IEatable), and round-shape(IShape). These identifications define your apple object in different way, though it is the same object. Now when a grocery store look at your apple, they will place in the fruit aisle (not caring which shape it would be). This is same when an graphic design look at the apple, they would only care how would render the shape of the apple.

So when you use Convert object. It does not really care what object it is as long as that object is convertible to another (by implementing IConvetable). And as you inherite IConvertable, you just have to implement method of conversion from given object to the new object.

Here is a reference to how to use IConvertable
 
Share this answer
 
http://forums.asp.net/t/1657293.aspx/1[^]

check this link..hope it will help...
 
Share this answer
 
Comments
Pravin2989 3-Aug-13 13:11pm    
Thanks for the link, but it dint help.

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