Search code examples
c#ms-solver-foundation

Exception: Cannot implicitly convert type 'Microsoft.SolverFoundation.Services.Term' to 'bool'


I'm getting an exception

"Cannot implicitly convert type 'Microsoft.SolverFoundation.Services.Term' to 'bool'"

in the following code:

double T1;
Decision T4;

var XX3 = T1 > (T4 - 0.001) ? T4 - 0.001 : T1;

How it is possible to fix this problem?


Solution

  • You use overloads of subtraction and greater than that have a return type Microsoft.SolverFoundation.Services.Term (see links). Then you use that expression x as the first part (of three) in the conditional operator x ? a : b. But a bool is required there.


    I suggest you use Model.If instead, it appears to be Solver Foundation's "conditional operator". To be technical, the C# language does not allow overloading the ?: ternary operator the way many binary operators like - and > can be overloaded.

    So change:

    var XX3 = T1 > (T4 - 0.001) /* illegal! */ ? T4 - 0.001 : T1;
    

    into:

    var XX3 = Model.If(T1 > T4 - 0.001, T4 - 0.001, T1);
    

    Disclaimer: I am not a Solver Foundation user.