2

Why does the following code have to have 'CalcUsable' defined using Explicit Interface Implementation syntax? (see last line of code) If I use non-explicit syntax, (ie. public decimal CalcUsable) I get an "inconsistent accessibility" error, specifically:

"Parameter type ...List (IVoucher) is less accessible than method CalcUsable(...)"

interface IVoucher
{
    string Serial { get; }
    decimal FaceValue { get; }
    string DiscountType { get; }
    string ApplyMsg { get; }
    string RejectMsg { get; }
    decimal CalcUsable(List<Product> products, List<IVoucher> vouchers);
}

// 'GiftVoucher' models the simple voucher which has no use restrictions.
//
public class GiftVoucher : IVoucher
{
    public string Serial { get; private set; }
    public decimal FaceValue { get; private set; }
    public string DiscountType { get; private set; }
    public string ApplyMsg { get; private set; }
    public string RejectMsg { get; private set; }

    public GiftVoucher(string serial, decimal faceValue)
    {
        Serial = serial;
        FaceValue = faceValue;
        ApplyMsg = string.Empty;
        RejectMsg = string.Empty;
        DiscountType = "Gift";
    }

    // 'CalcUsable' provides the voucher applicability logic.  
    //
    decimal IVoucher.CalcUsable(List<Product> products, List<IVoucher> vouchers)
    {
         blar, blar, blar...
steveax
  • 17,527
  • 6
  • 44
  • 59
CodeCabbie
  • 3,098
  • 2
  • 19
  • 30

1 Answers1

3

The class GiftVoucher is public whereas the interface IVoucher is not. As such, CalcUsable could not be called from anywhere where IVoucher is not available as the method takes a List of a type that isn't accessible.

As JRLambert suggested, making the interface public should solve this.

Adam H
  • 1,523
  • 11
  • 21
  • I thought the default accessibility for Interface was public? If not, what is it? – CodeCabbie Dec 13 '14 at 20:01
  • 2
    Default access modifier for the type in C# is `internal` (I'm not sure about nested types). – Dennis Dec 13 '14 at 20:06
  • It might well be better to remove the `public` modifier from the class (so as to make it `internal` just like the interface). – Jeppe Stig Nielsen Jan 26 '15 at 17:26
  • @CodeCabbie The default accessibility for a **member** (e.g. method) of an interface is `public`. It is the only allowed access level, so you are not even allowed to specify it. Example: Your declaration `decimal CalcUsable(List products, List vouchers);` inside the interface is `public`. The default access level of the `interface` itself depends on what the interface is a direct member of. If it is a direct member of a `namespace`, then the default accessibility of namespaces apply, which is `internal`. But if the interface is a member of a class or a struct, `private`. – Jeppe Stig Nielsen Jan 26 '15 at 17:32