Solved

Interface Method 1 to keep and 1 to override...

Posted on 2011-09-22
3
183 Views
Last Modified: 2012-06-21
Hello, I have a data connection wrapper I'm building to allow programs to act as the data source for another.

Here is the issue.  I have an interface, "IDataEngine", and in it, it has 2 methods.  (more, but it only needs 2 for this example of it)  
The Methods are,

public DataSet QuerySet(string sql); // inheriting classes should need to override this;

public DataTable QueryTable(string sql) // inheriting classes should not need to override this.
{
  // does error checking but is essentially,
  return QuerySet(sql).Tables[0];
}

Clearly, I do not need EVERY inheriting app to rewrite the same code for QueryTable, since it will be the same EVERY time.  

But so far, every time I tell a class to implement the IDataEngine interface, it overrides the methods that have code as well as the ones without.

How do I change this, so overriding classes do not need to rewrite methods that are already functional on an interface?  Thanks.
0
Comment
Question by:hpdvs2
  • 2
3 Comments
 
LVL 8

Author Comment

by:hpdvs2
Comment Utility
I just resolved it one way, but using an abstract class, and marking only the few methods, like QuerySet to abstract as well.

Despite that, I'm sure I've seen this done with an interface, but its dificult finding this kind of information by searching.

Anyone know if this can be done with an interface?  (without producing static methods that the overiding methods can call?)
0
 
LVL 4

Accepted Solution

by:
theHollow earned 500 total points
Comment Utility
Hello
Interfaces cannot hold code. I don't think that you have seen this anywhere :-)
So what you need to do, is what it seens that you have already done.

You have to make base class for this, instead of an interface.
 - The class itself should be marked with "abstract".
 - The method that should be overridden (QuerySet) should be marked as "abstract" as well, and hold no code
 - The method that should not be overridden (QueryTable) should not be marked with "abstract"

The abstract methods and properties will be generating a build error if not implemented when you inherit from the base class.

None of these methods needs to be marked as "static" unless you want them to be available from the class itelf without being instanced.

Hope that clearified a bit :-)
0
 
LVL 8

Author Comment

by:hpdvs2
Comment Utility
And I'm also finding validation on this as well.  
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now