3

I have an interface with overloaded methods.

interface ISide { Dictionary<string, decimal> Side(string side1, decimal cost1); Dictionary<string, decimal> Side(string side1, decimal cost1, string side2, decimal cost2); } 

I would like to implement only one of these depending on which class is inheriting it, but I am getting a compiler error by attempting to implement only one of these methods per class.

class Entree: ISide { public Dictionary<string, decimal> Side(string side1, decimal cost1, string side2, decimal cost2); } 

In this situation, do I have to use optional parameters to achieve what I'm trying to do here?

4
  • Mind sharing a bit more info about the context here? It could help giving you best advice. :) Commented Mar 6, 2014 at 21:22
  • 2
    Edit the question and use the actual interface names. That will make your code's context a tad more obvious to anyone reading your post. :) Commented Mar 6, 2014 at 21:25
  • Edited to show implementation. Commented Mar 6, 2014 at 21:28
  • Dictionary<string, decimal> Side(IEnumerable<Tuple<string, decimal>>sideCost); Commented Mar 6, 2014 at 21:32

5 Answers 5

7
interface ISomething1 { int SomeMethod(int someInt1); } interface ISomething2 { int SomeMethod(int someInt1, int someInt2); } class Someclass1 : ISomething1 { public int SomeMethod(int someInt1); } class Someclass2 : ISomething2 { int SomeMethod(int someInt1, int someInt2); } class Someclass3 : ISomething1, ISomething2 { public int SomeMethod(int someInt1); int SomeMethod(int someInt1, int someInt2); } 
Sign up to request clarification or add additional context in comments.

1 Comment

This is a correct answer given the context of the original question.
6

You should turn the concept of string side1, decimal cost1 into an object.

public class MenuItem { public string Name {get; set;} public decimal Cost {get; set;} } 

Then your interface takes a list of MenuItem

interface ISide { Dictionary<string, decimal> Side(IEnumerable<MenuItem> sides); } 

Now, this being said. It seems weird to me that Entree is an ISide. You should try to be a little more specific about what that interface means (maybe IComeWithSides or something)

7 Comments

The actual implementation is adding sides to a dish. We know that entrees generally come with two sides, but sandwiches usually only come with one. Other dishes come with no sides(in which case, the interface is not inherited.) Splitting that up would seem frivolous, no?
Then your interface should take in a collection object as the parameter.
I tend to agree, although it would be much easier if OP gave more details on the context. It's kinda hard to give good advice over ISomething :p
But the solution to your "problem" changed completely when you gave more context. The original answer was completely unrelated because we didn't know the context.
Just to add to @Caleb advice: your interface should not use dictionaries as public members. Create an additional class that will represent what the Side method is supposed to return.
|
5

You can't do it like that.You have to implement all methods defined in your interface.But instead you can use params keyword if only your parameter count changes.

interface ISomething { int SomeMethod(params int[] numbers); } 

If you want to require at least one parameter then you can do the following:

 int SomeMethod(int x, params int[] numbers); 

Comments

2

In order to implement an interface you have to implement all methods. Either do that, or use a single method with an optional parameter.

Comments

0

When a class implements an interfaces, you must implement all the methods from the interface. If you want to implement only a few methods use an abstract class.

2 Comments

Even the abstract class must implement the interface in full. Using an abstract class in this case doesn't really resolve anything, in my opinion. :)
The point that I was trying to make was that he may use an abstract class instead of an interface :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.