Daumantas Drukteinis
Daumantas Drukteinis

Reputation: 45

Ternary operator not working in if or statement

I have if or statement in which I put some of my conditions and a ternary operator. I've noticed that if I put ?: operator my if statement always go false. I'm wondering what causes it, because true condition comes before evaluating ?: operator so therefore it shouldn't even check it.

row.number == barcode -> false;
row.eancode == barcode -> true;
row.packageean == barcode -> false;
row.IspackingcodeNull() -> true;


if (row.number == barcode
    || row.eancode == barcode
    || row.packageean == barcode
    || row.IspackingcodeNull() ? false : row.packingcode == barcode
    || row.producerproductcode == barcode
    || row.alternativebarcode1 == barcode)

Upvotes: 0

Views: 202

Answers (1)

Matthew Watson
Matthew Watson

Reputation: 109557

What you've actually done is this:

if 
(
    (
        row.number == barcode
        || row.eancode == barcode
        || row.packageean == barcode
        || row.IspackingcodeNull()
    ) 
    ? false 
    : row.packingcode == barcode || row.producerproductcode == barcode || row.alternativebarcode1 == barcode
)

What you want is this:

if (row.number == barcode
    || row.eancode == barcode
    || row.packageean == barcode
    || (row.IspackingcodeNull() ? false : row.packingcode == barcode)
    || row.producerproductcode == barcode
    || row.alternativebarcode1 == barcode)

Upvotes: 4

Related Questions