Ash Robinson
Ash Robinson

Reputation: 61

IF condition not met but code still being triggered

I've come across something really odd which I've never seen before and wondered if anyone could shed light on the issue:

The old code:

if ((orderFileCreationStatus.OrderFileStatus == OrderFileCreationStatus.Status.ContentCreated
        || orderFileCreationStatus.OrderFileStatus == OrderFileCreationStatus.Status.NoContent)
    && orderFileCreationStatus.SpecialMeasureFileStatus != OrderFileCreationStatus.Status.Published
    && orderFileCreationStatus.PersonalisedProductFileStatus != OrderFileCreationStatus.Status.Published)
{
    webOrderHeader.OrderStatus = Domain.Duos.OrderStatus.AwaitingAuthorisation;
    webOrderHeader.AuthoriserUserId = null;
    UpdateWebOrderHeader(webOrderHeader);

    throw new Exception("Could not create the Order File for order ID: " + webOrderHeader.OrderId.ToString() + "rollback");
}

The new code:

if ((orderFileCreationStatus.OrderFileStatus == OrderFileCreationStatus.Status.ContentCreated || orderFileCreationStatus.OrderFileStatus == OrderFileCreationStatus.Status.NoContent))
{
    if(orderFileCreationStatus.SpecialMeasureFileStatus != OrderFileCreationStatus.Status.Published)
    {
        if(orderFileCreationStatus.PersonalisedProductFileStatus != OrderFileCreationStatus.Status.Published)
        {
            webOrderHeader.OrderStatus = Domain.Duos.OrderStatus.AwaitingAuthorisation;
            webOrderHeader.AuthoriserUserId = null;
            UpdateWebOrderHeader(webOrderHeader);

            throw new Exception("Could not create the Order File for order ID: " + webOrderHeader.OrderId.ToString() + "rollback");
        }
    }
}               

Now as far as resharper is concerned these two if statements are identical but here's the catch.

In the old code the debugger is skipping the first three lines nested in the IF and throwing the exception.

In the new code this works perfectly. Has anyone experienced this before and more importantly what is causing it. I'm intrigued to find out.

Upvotes: 0

Views: 299

Answers (2)

Haney
Haney

Reputation: 34832

The statements are identical. The issue must lie in the version of code being run. Try breaking the If block into a new Console app and see if you can reproduce the differences again:

static void Main(string[] args)
{
    bool A = true;
    bool B = false;
    bool C = true;
    bool D = true;

    bool result1 = (A || B) && C && D;
    Console.WriteLine("Old way: " + result1);

    bool result2 = false;
    if (A || B)
    {
        if (C)
        {
            if (D)
            {
                result2 = true;
            }
        }
    }

    Console.WriteLine("New way: " + result2);

    Console.ReadKey();
}

Upvotes: 0

Haney
Haney

Reputation: 34832

This logic will never be true:

if(orderFileCreationStatus.SpecialMeasureFileStatus != orderFileCreationStatus.SpecialMeasureFileStatus)

You're comparing the value type to itself, which will always be true, and then negating that true to false at all times.

Upvotes: 2

Related Questions