Solved

Change return type in Interface Method of a Class (C#)

Posted on 2009-05-15
3
524 Views
Last Modified: 2012-05-07
I'm in the process of building a set of custom classes to wrap up a custom OLEDB provider, so I'm recreating classes such as MyDbConnection, MyDbCommand etc.  The problem is that when using an interface, such as IDbConnection, it returns an interface type.

For example, I have a MyDbCommand class (using IDbCommand) and a MyDbConnection class (using IDbConnection).  When I provide an method for the MyDbConnection.CreateCommand() it returns a IDbCommand, not a MyDbCommand as I want it to.

When I look at the 'standard' classes such as OleDbConnection, it's CreateCommand() method returns an OleDbCommand object, not a IDbCommand object so I'm guessing this is possible?

The method in the MyDbConnection as the moment looks like:

public IDbCommand CreateCommand()
{
    return new MyDbCommand();
}

which compiles, but then requires conversion when used in another project.  Am I missing a keyword/method in order to do this?
0
Comment
Question by:d_brentley
  • 2
3 Comments
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24395658
we are using something what you are trying to do but we are using the enterprise library to achieve the functionality

we achieve something like this
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24395691
protected Database Create (string databaseInstance)
{
      m_dbProvider = ConfigurationManager.ConnectionStrings[databaseInstance].ProviderName;
      switch (m_dbProvider)
      {
            case DbProviderMapping.DefaultOracleProviderName:
            return new OracleDatabase (ConfigurationManager.ConnectionStrings[databaseInstance].ConnectionString);

            case DbProviderMapping.DefaultSqlProviderName:
            return new SqlDatabase (ConfigurationManager.ConnectionStrings[databaseInstance].ConnectionString);

            case DbProviderMapping.DefaultMySqlProviderName:
            return new MySqlDatabase (ConfigurationManager.ConnectionStrings[databaseInstance].ConnectionString);

            default:
            return DatabaseFactory.CreateDatabase (databaseInstance);
      }
}
0
 
LVL 9

Accepted Solution

by:
tculler earned 125 total points
ID: 24395928
Interfaces are set-in-stone, and they are that way or a very good reason. If you changed the return type on a global interface, yet then used it at a later time, you'd get that other return type; one that you may not want.

Instead, I would make a seperate method--one to satisfy the interface, and one to avoid the "casting" syntax. It is a good idea to still implement the interface, though.
public IDbCommand CreateCommand()
{
     // Shut the interface up, return your command
}
 
public MyDbCommand CreateMyCommand()
{
     // Return your command
}

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question