Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# 2.0 Return DataReader object
Message
From
10/03/2012 04:11:33
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01537785
Message ID:
01537892
Views:
38
>Hi,
>
>I have a method that needs to return either OleDbDataReader or SqlDataReader (depends on the back-end database). Within the method I have IF/ELSE where the code is processed accordingly. But in the method signature I can only specify one type of reader. E.g.
>
>public OleDbDataReader MethodName()
>or
>public SqlDataReader MethodName()
>
>
>Does it mean that I have to split the method into two methods depending on the type of reader I need to return and deal with it at the calling level?

It depends on whether you need to use any methods or properties that are specific to one or the other. If you don't then you can reference either as IDataReader. eg:
IDataReader dataReader = GetDataReader();

       private IDataReader GetDataReader()
        {
            if (YouWantAnSqlReader)
                return new SqlDataReader();
            else
                return new OleDbDataReader();
        }
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform