Solved

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

Posted on 2011-09-22
3
188 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 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 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