0

I define an interface and build a class inherit it.

interface IOperator
{
    int Priority{ get; }
    string Identifier { get; }
    int Dimension { get; }

    Expression GetExpression(IVariable[] variables);
    object Excute(IVariable[] variables);

}

public class Op_Muti:IOperator
{
    int IOperator.Priority { get { return 40; } }
    string IOperator.Identifier { get { return "*"; } }
    int IOperator.Dimension { get { return 2; } }


public object IOperator.Excute(IVariable[] variables)
{
    throw new ArgumentOutOfRangeException(string.Format("{0} can't apply to {1}", Identifier, variables[0].VarType.ToString()));
}

}

The Identifier is defined in interface

string Identifier { get; }

and implement in class Op_Muti

string IOperator.Identifier { get { return "*"; } }

but can't reference in

throw new ArgumentOutOfRangeException(string.Format("{0} can't apply to {1}", Identifier, variables[0].VarType.ToString()));

Why?

user2155362
  • 1,657
  • 5
  • 18
  • 30
  • @rawling incorrect duplicate. OP has no problem with the other 4 members and there is only 1 inheritance –  Feb 15 '19 at 14:45
  • In your `Execute` method you probably need to use `IOperator.Identifier` instead not just refer to it as `Identifier` –  Feb 15 '19 at 14:48
  • 2
    Why are you using _explicit_ notation anyway? For a single interface? –  Feb 15 '19 at 14:50
  • 2
    @MickyD Disagree. OP has implemented 3 interface properties explicitly, is trying to implement the method explicitly, and is wondering why he can't access one of the explicitly-implemented interface properties. Sounds like OP needs to understand how implicit and explicit implementation of interface properties works? (... which is exactly what you've put in your followup comments...) – Rawling Feb 15 '19 at 15:12
  • 2
    @Rawling the point I'm making is that whilst the answers explain well the concept of how to define and invoke a class with explicit members, none point out that _in order for a class member to invoke/refer-to an explicity-defined member, that `it must use the interface prefix too`_. From within the class. That's unexpected. That was something I didn't know until I looked at this question. _So I learnt something new today_ –  Feb 16 '19 at 02:54
  • explicit interface implementations is something I would recommend against, unless there's a good reason for it. – Zohar Peled Feb 18 '19 at 09:34

0 Answers0