Casting an Object type to DataRow & IDatareader
Asked
Active
Viewed 745 times
2 Answers
1
You cant just cast an object to either of those you mentioned. They have to be either of those or derive from those types.
There is no alchemy in programming.

leppie
- 115,091
- 17
- 196
- 297
-
object reader1 reader1 = Convert.ChangeType(reader1, Type.GetType(reader1.ToString())); //(object)Convert.ChangeType(MyDataSet, typeof(object)); //(System.Data.DataRow)Convert.ChangeType(reader1, Type.GetType(reader1.ToString())); //(Type.GetType(reader1))Convert.ChangeType(reader1, Type.GetType(reader1.ToString())); – Charu Aug 12 '10 at 05:30
-
i thing, we can do like the above example – Charu Aug 12 '10 at 05:31
-
@Charu: No, that is very wishful thinking. – leppie Aug 12 '10 at 06:35
1
Casting can only happen between compatible types. To transform two incompatible types you need to perform conversion.
Ex: You cannot cast a string into int and vice versa since they are incompatible types so there is a way of converting both into one another.
int x = 5;
string str = string.Empty;
str = x.ToString(); //Convert int x into string str.
x = Convert.ToInt32(str); //Convert string str into int x.
Instead of writing the whole story explaining difference between casting and conversion here, I'd rather recommend you to read this interesting conversation on SO. Do read Eric Lippert's answer. Eric Lippert works with Microsoft in C# compiler development team.

Community
- 1
- 1

this. __curious_geek
- 42,787
- 22
- 113
- 137