Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2009-05-15
3
Medium Priority
?
549 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 375 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

610 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