Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-09-22
3
Medium Priority
?
194 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
ID: 36582956
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 2000 total points
ID: 36583311
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
ID: 36599746
And I'm also finding validation on this as well.  
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

971 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