Optimizing if else statements

studio

#1

Hi all

i had to loop through a datatable and check if cells matches certain values:
1-Read the second row cell(indexed by it’s colum name)
2-For each type found, eg: type1, type2: do a check in other cells (indexed with different colum name)
I’m using if statement and the treatment took 2 minute, i want to reduce this time:

row(“Type “).ToString.Contains(”[A]”) OR row(“Type “).ToString.Contains(”[B]”) OR row(“Type “).ToString.Contains(”[C”) OR row(“Type “).ToString.Contains(”[D]”) OR row(“Type “).ToString.Contains(”[E]”)


#2

If you want cleaner code, use Switch (select).

But if you still have a specific case that may require to execute as few if conditions as possible, use some sort of logic tree to arrange your ifs.

if (c == ‘a’)…

if (c == ‘d’)…

if (c == ‘i’)…

and so on can be rearranged

if (c <= ‘q’)

{

if (c <= ‘k’)

{


#3

@abdel, Try with LinQ

will convert your filtered table to a dataTable


#4

Hello you just try switch cases.

if (i == 1) {

// action 1

} else {

if (i == 2) {

    // action 2

} else {

    if (i == 3) {

        // action 3

    } else {

        // action 4

    }

}

}

switch (i) {

case 1:

    // action 1

    break;

case 2:

    // action 2

    break;

case 3:

    // action 3

    break;

default:

    // action 4

    break;

}
Kodi Mobdro