Search code examples
c#interfaceexplicit-interface

Explicit Interface Implementation demanded, but only one interface in use?


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...

Solution

  • 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.