Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends...

Can any tell me is it possible to cast a Reader class in User defined Class

For e.x.
Suppose i have object of reader which have information of employee and i want to cast it in employee class object. Is it possible.


Thanks.
Posted

1 solution

It's possible, but only when your reader is either descendant of Employee class or realizes the same interface:
1)
C#
public class Employee
{
...
}

public class Reader: Employee
{
...
}


2)
C#
interface IEmployee
{
...
}

public class Employee: IEmployee
{

...
}

public class Reader: IEmployee
{
...
}
 
Share this answer
 
Comments
Varinder Raii 9-Jun-12 2:35am    
Here reader is SqlReader
Timberbird 9-Jun-12 2:46am    
Well, it complicates things. I think you'll have to create a new class derived from SqlReader and add IEmployee interface realization to that class. Another option I initially forgot, you could overload cast operator, but that also requires new class derived from SqlReader to be created
Varinder Raii 9-Jun-12 5:41am    
Actually i want to make a common method for master forms

which will retrieve data from database for all master forms. but problem is that

when reader retrieve data then how can i add this data to list.

For Ex.

Collapse | Copy Code

public static List<object> Retrieve(string TableName)
{
List<object> List = null;
using (var con = ClsCommon.ConnectionOpen())
{
using (var transaction = con.BeginTransaction())
{
string Qry = "Select * from "+TableName +"";
SqlCommand command = new SqlCommand(Qry ,con );
SqlDataReader Reader = command.ExecuteReader ();
if (Reader.HasRows )
{
while (Reader.Read ())
{
// Now here how can i add values from reader to List
}
}
}
}
return null;
}


I want to make a common method. is there any way for this problem. using Genric Class or any other thing.

Please help me i will be very thankful to you guys.

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