Solved

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

Posted on 2009-05-15
3
535 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

730 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