tags:

views:

420

answers:

5

Hello,

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?

+4  A: 

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

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);
}
Botz3000
A (well formed) interface can only have public methods. Interestingly, the CLR supports constants, fields, and even static (type) constructors for interfaces. This is because an interface is implemented just as any other type definition.
Robert Venables
Fields on interfaces are not CLS-Compliant though, no?
Botz3000
Correct. CLS-compliant interfaces cannot contain static members because languages such as c# are not able to define or access them.
Robert Venables
+9  A: 

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.

I'm surprised that you can even have private methods as part of an interface in C#.

Kim Gräsman
You can't. Specifying an access modifier (private or public) results in a compilation error.
JulianR
Thanks, that rings a bell.
Kim Gräsman
A: 

You should probably check out the MSDN article here about interfaces: http://msdn.microsoft.com/en-us/library/ms173156.aspx

Robban
+1  A: 

An interface cannot contain private fields. However, the closest behavior to "private fields" you can achieve is by using explicit implementation (explained here: http://msdn.microsoft.com/en-us/library/ms173157.aspx)

A: 

Rule of interface

The CLR also allows an interface to contain static
methods, static fields, constants, and static constructors. However, a CLS-compliant
interface must not have any of these static members because some programming languages
aren’t able to define or access them. In fact, C# prevents an interface from defining any
static members. In addition, the CLR doesn’t allow an interface to contain any instance fields
or instance constructors.

These are the rules of interface you cant do anything on that :)

these are not allowed

interface IIValidationCRUD
{
    static ICRUDValidation IsValid(object obj); //error
}

interface IIValidationCRUD
{
    public ICRUDValidation IsValid(object obj); //error
}
anishmarokey