Our Man in Bananas
Our Man in Bananas

Reputation: 5981

c# Ternary operator

I am working on a an application that has no documentation (although the code is quite clear and well-written) and I am trying to write some useful technical documentation for the next guy who comes along after I am in a mental hospital.

In the web-service method, if the call to the web-service returns an error, then the Catch code runs to increment the number of retries for the message and set the MessageStatus to "New" (so that it is retried if less than 5) or "Error" (for unknown errors), but there is one line I am not completely sure of and I need to document this process properly:

catch (Exception ex)
{
int NoRetries = (int)dRow[(int)Common.OutboundSQLFields.Message_Retries];
string messageStatus = (NoRetries < 5) ? Common.MessageStatus(ex) : "Expired";
    ...

Does this mean that if the NoRetries is greater than 4, then the MessageStatus will be set to Expired, else the method Common.MessageStatus will be called to reset the string MessageStatus based on the value of (ex) ?

So to make it more self-describing, could I rewrite that Ternary operator code as:

string MessageStatus="";
If (NoRetries > 4)
    {
    MessageStatus = "Expired";
    }
else
    {
    MessageStatus = Common.MessageStatus(ex);
    }

Upvotes: 0

Views: 204

Answers (2)

Johan
Johan

Reputation: 262

(Statement) ? TRUE : FALSE

So if NoRetries < 5 then Common.MessageStatus(ex) if NoRetries >= 5 then "Expired"

Hope this clarifies it :)

Upvotes: 4

Shaharyar
Shaharyar

Reputation: 12439

As the statement is:

string messageStatus = (NoRetries < 5) ? Common.MessageStatus(ex) : "Expired";

It should be like:

string MessageStatus="";
if (NoRetries < 5)
    MessageStatus = Common.MessageStatus(ex);
else
    MessageStatus = "Expired";

You reversed it actually. But your's is correct too.

Upvotes: 1

Related Questions