ASP.NET MVC Get the latest date even if it has the same date

The code below shows the status of the recent date. It compares if the first date is recent than the second date, and so on. But I have a problem. What if the first date and the second date are the same?

For example, the first date is 2013-04-15 (a_mm_date) and the second date is 2013-04-15 (a_uat_date). a_mp_date is null so it's ok. So it goes like this: a_mm_date -> a_mp_date -> a_uat_date. The system will show the status as "FOR APPROVAL MM". But is should show the "IN PROCESS UAT" as status not the FOR APPROVAL MM.

Any idea guys on how to do this?

//Get values if the status is A

if (data.s == "A")
{
    if (data.a_mm_date >= data.a_mp_date && data.a_mm_date >= data.a_uat_date)
    {
        data.s = "For Approval MM";
    }

    if (data.a_mp_date >= data.a_mm_date && data.a_mp_date >= data.a_uat_date)
    {
       data.s = "For Approval MP";
    }

    if (data.a_uat_date >= data.a_mp_date && data.a_uat_date >= data.a_mm_date)
    {
       data.s = "In Process UAT";
    }
}
Jon Skeet
people
quotationmark

I found your question hard to follow, but I suspect the simplest approach is just to use else:

if (...)
{
    data.s = "For approval MM";
}
else if (...)
{
    data.s = "For approval MP";
}
else if (...)
{
    data.s = "For approval UAT";
}
else
{
    // Whatever you want if none of the above is met
}

I'd then extract that into a separate method, which will stop you from needing to repeat the data part 16 times:

if (data.s == "A")
{
    data.s = ComputeNewApproval(data.a_mm_date, data.a_mp_date,
                                data.data.a_uat_date);
}

...

private static string ComputeNewApproval(DateTime mmDate, DateTime mpDate,
                                         DateTime uatDate)
{
    ...
}

(Make them DateTime? if you need to.)

I'd then change the names of the properties to be more conventional - if s is meant to mean status, then make the property Status... and ideally get rid of the underscores, expanding MM, MP and UAT into more meaningful names. (I'm guessing that UAT is user acceptance testing, but I've no idea what MM and MP are.)

people

See more on this question at Stackoverflow