vendredi 17 septembre 2021

C# : IF ELSE replacement

In the application there are 5 methods,in all methods there are update queries. Methods are dependent on each other. If first method runs successfully, second is called; second runs successfully - third is called; third runs successfully - fourth is called; fourth runs successfully - fifth is called.

Here is code I have written

public void method1()    
{    
   int result = cmd.ExecuteNonQuery();    

   if(result > 0)    
   {    
        method2();    
   }    
}

public void method2()    
{    
    int result = cmd.ExecuteNonQuery();

    if(result > 0)    
    {    
        method3();    
    }    
}

public void method3()    
{    
    int result = cmd.ExecuteNonQuery();
 
    if(result > 0)    
    {    
        method4();    
    }    
}

public void method4()    
{    
    int result = cmd.ExecuteNonQuery();

    if(result > 0)  
    {    
        method5();    
    }    
}

I want to write this same code without using if statement, switch case or ternary operators. please guide me on how to do this.

Aucun commentaire:

Enregistrer un commentaire