Code
Code

Reputation: 356

C# IF statement condition execution order (VS2010)

I have an if statement with 4 conditions:

if (Global.LoggedAdmin != null &&
    LaunchDataGridView.CurrentCell.ColumnIndex == 5 ||
    LaunchDataGridView.CurrentCell.ColumnIndex == 6 ||
    LaunchDataGridView.CurrentCell.ColumnIndex == 9 )
                 {
                   //code
                 }

If the column index is 5, for some reason it doesn't execude the code, with the rest of the column indexes it works, I knew that if you have a || b || c ||....|| z, if at least one of them is true, the code should execute. LoggedAdmin is always NOT null. I tried putting the column indexes conditions 1'st , and the LoggedAdmin condition last, inside the if brackets, then the code executes if column index is 5 || 6 but not when it's 9, is this normal? and if yes, why? PS: I have another IF statement with 3 arguments that is more or less like the one above, there it failed one of the conditions ( column indexes condition ) too but when I put the column index part in brackets it worked) , for the example above, if I put them in brackets it doesn't work with any of the column indexes :/, I don't understand anything anymore.

Upvotes: 0

Views: 139

Answers (5)

Tim Schmelter
Tim Schmelter

Reputation: 460138

The && has a higher precedence than the || operator, that's why && is evaluated first:

Global.LoggedAdmin != null && LaunchDataGridView.CurrentCell.ColumnIndex == 5 ...

You can see the precendences here: http://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx

That means that Global.LoggedAdmin is null, otherwise it would also be executed if the index is 5. You either have to use also || instead of &&:

if (Global.LoggedAdmin != null ||
    LaunchDataGridView.CurrentCell.ColumnIndex == 5 ||
    LaunchDataGridView.CurrentCell.ColumnIndex == 6 ||
    LaunchDataGridView.CurrentCell.ColumnIndex == 9 )
 {
   //code
 }

or you need to use == null and wrap the rest in round brackets. If LoggedAdmin is always null as you've mentioned that could make sense:

if (Global.LoggedAdmin == null && (
    LaunchDataGridView.CurrentCell.ColumnIndex == 5 ||
    LaunchDataGridView.CurrentCell.ColumnIndex == 6 ||
    LaunchDataGridView.CurrentCell.ColumnIndex == 9 ))
 {
   //code
 }

Update:

LoggedAdmin is always NOT null, I made a error there in writing that

Than this might be code for you:

if (Global.LoggedAdmin != null && (
    LaunchDataGridView.CurrentCell.ColumnIndex == 5 ||
    LaunchDataGridView.CurrentCell.ColumnIndex == 6 ||
    LaunchDataGridView.CurrentCell.ColumnIndex == 9 ))
 {
   //code
 }

Upvotes: 1

Kjartan
Kjartan

Reputation: 19111

Your question has in principle already been answered multiple times, but I`d still add a little detail for clarity, both for you, and for anybody else who might read your code later:

You can think of your if-clause as doing two tests: One for whether or not Global.LoggedAdmin != null, and another for whether ColumnIndex is one of your specified values.

For clarity, you could split this as follows; as a result, your initial problem would also disapear:

if (Global.LoggedAdmin != null && IsRelevantColumn())
{
    //code
}


private static bool IsRelevantColumn(){
    return LaunchDataGridView.CurrentCell.ColumnIndex == 5 ||
           LaunchDataGridView.CurrentCell.ColumnIndex == 6 ||
           LaunchDataGridView.CurrentCell.ColumnIndex == 9;
}

Upvotes: 1

Hyarantar
Hyarantar

Reputation: 217

You're saying that Global.LoggedAdmin is always null. Then you have to do something like that :

if (Global.LoggedAdmin == null &&
LaunchDataGridView.CurrentCell.ColumnIndex == 5 ||
LaunchDataGridView.CurrentCell.ColumnIndex == 6 ||
LaunchDataGridView.CurrentCell.ColumnIndex == 9 )
{
   //code
}

Or, you may do something like that :

if (Global.LoggedAdmin == null &&
(LaunchDataGridView.CurrentCell.ColumnIndex == 5 ||
LaunchDataGridView.CurrentCell.ColumnIndex == 6 ||
LaunchDataGridView.CurrentCell.ColumnIndex == 9 ))
{
   //code
}

Upvotes: 0

Sai Avinash
Sai Avinash

Reputation: 4753

As you know, Parenthesis will have higher priority.

if (Global.LoggedAdmin != null &&
        LaunchDataGridView.CurrentCell.ColumnIndex == 5 ||
        LaunchDataGridView.CurrentCell.ColumnIndex == 6 ||
        LaunchDataGridView.CurrentCell.ColumnIndex == 9 )
                     {
                       //code
                     }

is equivalent to

if ((Global.LoggedAdmin != null) &&
    (LaunchDataGridView.CurrentCell.ColumnIndex == 5 ||
    LaunchDataGridView.CurrentCell.ColumnIndex == 6 ||
    LaunchDataGridView.CurrentCell.ColumnIndex == 9) )
                 {
                   //code
                 }

Upvotes: 0

Fun Mun Pieng
Fun Mun Pieng

Reputation: 6891

I suspect Global.LoggedAdmin might actually be null and the expression evaluated is:

(Global.LoggedAdmin != null && LaunchDataGridView.CurrentCell.ColumnIndex == 5)
 ||
LaunchDataGridView.CurrentCell.ColumnIndex == 6
 ||
LaunchDataGridView.CurrentCell.ColumnIndex == 9

I think you might actually mean:-

Global.LoggedAdmin != null && (
    LaunchDataGridView.CurrentCell.ColumnIndex == 5 ||
    LaunchDataGridView.CurrentCell.ColumnIndex == 6 ||
    LaunchDataGridView.CurrentCell.ColumnIndex == 9)

which will still not execute the code since Global.LoggedAdmin is null.

Upvotes: 1

Related Questions