vendredi 15 mars 2019

Converting ternary operator to if-else statement

I'm having a hard time trying to convert the following C# code into a if-else statement, so that I can understand the business logic. Can anybody help me, checking whether my convertion is ok?

Code:

dateInit = objInstance == null ? (DateTime?)null:
                objInstance.DateAnt == null ?
                    objInstance.DatePost > otherObjInstance.DateCon ?
                        (DateTime?)null :
                        objInstance.DatePost :
                objInstance.DateAnt;

My convertion:

if(objInstance == null)
   {
      dateInit = (DateTime?)null;
   }
   else
   {
      if(objInstance.DateAnt == null)
        {
        if(objInstance.DatePost > otherObjInstance.DateCon)
        {
            dateInit = (DateTime?)null;
        }
        else
        {
        dateInit = objInstance.DatePost;
        }
        }
    else
    {
      dateInit = objInstance.DataAnt;
    }
   }

Aucun commentaire:

Enregistrer un commentaire