1

I want to use condition expression to choose lambda expression, like that:

xxxx.UsingFactory(
hasProofing? ( ()=>new ProofingA() ) : ( () => new ProofingB() )
);

But, it show me errors. So, if I want to do this thing, How should I do.

Error Detail:

no implicit conversion between 'lambda expression' and 'lambda expression'

v11
  • 2,124
  • 7
  • 26
  • 54
  • 1
    *show me errors* .. It is very helpful if you state those errors. Also, show the signature of the `UsingFactory`, what does it accept as parameters ? Do `ProofingA` and `ProofingB` inherit from the same object ? – Zein Makki Sep 05 '16 at 05:28
  • implement from the same interface – v11 Sep 05 '16 at 05:32
  • Also, what `xxx.UsingFactory()` takes as parameter? may be it does not take an Action without parameters? try `x=>new ProfingA()` – Alex Sep 05 '16 at 05:33
  • You have to explicitly cast your lambdas. See [this](http://stackoverflow.com/q/5490095) question, or maybe [these](http://stackoverflow.com/q/8506733) [ones](http://stackoverflow.com/q/11309557) –  Sep 05 '16 at 05:35
  • @v11 And does your method accept `Func` or accepts what exactly ? Just an `Action` ? – Zein Makki Sep 05 '16 at 05:39
  • @user3185569 I think just action, and I have solved this problem via using if /else to choose return new proofingA() or proofingB() – v11 Sep 05 '16 at 05:41

1 Answers1

2

You need to explicitly cast at least one of the lambdas. For example, if it's just a Action, then you could use the following:

xxxx.UsingFactory(
    hasProofing ? (Action)(() => new ProofingA()) : () => new ProofingB()
);
grovesNL
  • 6,016
  • 2
  • 20
  • 32