Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a method private in an interface?

Tags:

c#

interface

I have this interface:

public interface IValidationCRUD {     public ICRUDValidation IsValid(object obj);     private void AddError(ICRUDError error); } 

But when I use it (Implement Interface, automatic generation of code), I get this:

public class LanguageVAL : IValidationCRUD {        public ICRUDValidation IsValid(object obj)     {         throw new System.NotImplementedException();     }      public void AddError(ICRUDError error)     {         throw new System.NotImplementedException();     }    } 

The method AddError is public and not private as I wanted.

How can I change this?

like image 980
SmartStart Avatar asked Sep 05 '09 14:09

SmartStart


2 Answers

An interface can only have public methods. You might consider using an abstract base class with a protected abstract method AddError for this. The base class can then implement the IValidationCRUD interface, but only after you have removed the private method.

like this:

public interface IValidationCRUD {     ICRUDValidation IsValid(object obj); }  public abstract class ValidationCRUDBase: IValidationCRUD {     public abstract ICRUDValidation IsValid(object obj);     protected abstract void AddError(ICRUDError error); } 
like image 59
Botz3000 Avatar answered Oct 05 '22 23:10

Botz3000


A private member makes no sense as part of an interface. An interface is there to define a set of methods, a role, an object must always implement. Private methods, on the other hand, are implementation details, not intended for public consumption.

like image 34
Kim Gräsman Avatar answered Oct 05 '22 22:10

Kim Gräsman