Solved

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

Posted on 2009-05-15
3
540 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

724 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