Search code examples
c#ternary-operatorargb

ternary operator || Predefined type 'System.ValueTuple`3' is not defined or imported


private void Disable_Proxy_textboxes(bool v)
{
    v = !v;
    address_textbox.Enabled = v;
    port_textbox.Enabled = v;
    port_label.ForeColor = Color.FromArgb(v?(255,255,255):(100, 100, 100));
    address_label.ForeColor=v?(Color.FromArgb(255,255,255)):(Color.FromArgb(100,100,100));
}

While coding I decided to play around the ternary operators and now am really confused why there is an error in port_label.ForeColor = Color.FromArgb(v?(255,255,255):(100, 100, 100));. I understand that the next line is very correct but why does this specific line show the following errors:

Predefined type 'System.ValueTuple`3' is not defined or imported
cannot convert from '(int, int, int)' to 'int'  

The docs just say

Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.

So are both the data types not the same in my case? Are both the cases not same as this example?

bool c=a?true:false;
a?(c=true):(c=false);

What is it that I am not understanding. Also please this is not one of those why is this code not working, I am not asking any debugging here. Thanks.


Solution

  • The problem is not the conditional operator, both sides are of type ValueTuple`3. So the conditional operator ? returns a ValueTuple`3.

    But no overload of Color.FromArgb takes a ValueTuple`3 as argument.

    The compiler does not deconstruct the ValueTuple`3 into the three int arguments for the overload you intend to use.