MSVS 2012, C #

class main : BaseClass, IMainInterface {} 

How to prove to the compiler that the methods of the same name and from BaseClass and IMainInterface are one and the same and you don’t need to re-sculpt the implementation, that you only have to swear on new methods that should be on IMainInterface but not in BaseClass ?

Approximately what I want, while I must inherit from BaseClass (so as not to break compatibility with someone else's code)

 interface IMyInterface { void Print(string str); void SpecificIMethod(); } class BaseClass { public void Print(string str) { Console.WriteLine(str); } } class MyClassA : BaseClass, IMyInterface { public void SpecificIMethod() {} } class MyClassB : BaseClass, IMyInterface { public void SpecificIMethod() {} } class Program { static void Main(string[] args) { var myClass = (IMyInterface)new MyClassA(); myClass.Print("my message"); myClass.SpecificIMethod(); Console.ReadKey(); } } 
  • show the code - DreamChild
  • And doesn't this code work? - Mihey
  • SpecificIMethod declares, but in BaseClass it is not, fixed it - QuAzI
  • You do not put the question correctly. I'll try to clarify. Do you need to expand the base class? If you want to transfer your heirs to the places where IMyInterface is waiting, then this code is completely working. - Mihey

2 answers 2

Update the answer. You write this code:

  var myClass = (IMyInterface)new MyClassA(); myClass.Print("my message"); myClass.OtherPrint(); 

Then everything looks elementary - if the myClass variable is brought to the IMyInterface interface, this means that you have the right to call only those methods defined in IMyInterface in myClass - in general, an obvious thing. Otherwise (if the interface could call non-declared methods in it) the very concept of "interface" would be meaningless. In that case, you should probably declare the OtherPrint () method in your interface.

  • I try to simply indicate that BaseClass and the MainClass inherited from it have methods and properties declared in the interface. Linked. When executing an error, public class MainClass: BaseClass {} var test = (Helper.IMainInterface) new MainClass (); // Unable to cast object of type 'MainClass' to type 'IMainInterface'. I specify that MainClass is still from the base class AND interface public class MainClass: BaseClass, IMainInterface {} It doesn’t even link to each method and property of mathes in the spirit of 'MainClass' does not implement interface member 'Helper.IMainInterface.myField' C: \ devel \ test \ MainClass.cs - QuAzI
  • @QuAzI what's stopping you from showing the whole code? Why do you bring any scraps? Show your inheritance hierarchy, show your interface, show how you use them, how you get an error. So far, your problem is really difficult to understand without having an idea about what you are writing - DreamChild
  • in general, your mistake says that you are not implementing an interface contract. If there are any methods or properties in the interface, then all of them must be present in the class that implements the interface. This is actually the meaning of interfaces - to be sure that the class implementing this interface can do this and that - DreamChild
  • DreamChild, I tried to interface it into an already working rather large project, by itself, I cannot show its original sources - QuAzI
  • Well, show the method signatures, you can replace the method names with fake ones. In general, simulate the same situation on any test classes and interfaces so that you can judge exactly what kind of error you have. Most likely, you simply did not implement all interface properties / methods in your class, so when you try to cast an instance to an interface, you get an exception in runtime, and when you try to implement an interface in a class, you get a compilation error - DreamChild
 interface IMyInterface { void Print(string str); void OtherPrint(string str); } class BaseClass { public void Print(string str) { Console.WriteLine(str); } } class MyClass : BaseClass, IMyInterface { public void OtherPrint(string str) { throw new NotImplementedException(); } } class Program { static void Main(string[] args) { var myClass = (IMyInterface)new MyClass(); myClass.Print("my message"); myClass.OtherPrint("second message"); Console.ReadKey(); } } 
  • BaseClass was not written by me, respectively, it is not inherited from the interface, it is just stuffed with these methods. - QuAzI
  • > If the methods of the same name and property in BaseClass and IMainInterface are the same, then BaseClass must implement IMainInterface in the class there cannot be properties and methods of the same nameDreamChild
  • This means not the properties with the names of the methods, but the presence in BaseClass of the same list of properties and methods that IMainInterface declares - QuAzI
  • I ask you again - give the code. You have explained very poorly what exactly you need. Guessing on the coffee grounds - the lesson is not particularly interesting. However, the presence in the class of the heir of the methods available in the ancestor and in the implemented interface should not be a problem. The compiler will not swear at this - DreamChild
  • How correctly I understand you, then you write IMainInterface exactly, then why not call the methods and properties of the interface differently so that they differ from BaseClass, then there is an option to inherit the main class from both BaseClass and IMainInterface. I will give the code in the answer above. - Mihey