user755579
user755579

Reputation: 11

If statements to check every permutation

Hi I am trying to evaluate a list of 3 data records coming back from database.

I need to check if these values are null & replace with a "-" symbol.

The problem I end up is having a very long list of if statements, is there an easy way to do this?

if (row count == 1)
{
    if (only firstNull)
    {
        //Replace first.label with "-"
    }
    else if (only secondNull)
    {
        //Replace second.label with "-"
    }
    else if (only thirdNull)
    {
        //Replace third.label with "-"
    }
    else if (only (firstNull && secondNull))
    {
        //Replace first.label = "-" & second.label = ""
    } 
    else if (only (secondNull && thirdNull))
    {
        //Replace second & third label = "-"
    }
    else if (only (thirdNull && firstNull))
    {
        //Replace third && first label = "-"
    }
}
else
{
    // Replace all labels with "-"
}

Upvotes: 1

Views: 140

Answers (3)

forsvarir
forsvarir

Reputation: 10839

I don't understand why you have so many clauses... If a field is null, you want to replace it with "-"... You have 3 fields, you should at most have 3 clauses... (Note, this is pseudocode, to match your question)

if (row count == 1)
{
    if (firstNull)
    {
        //Replace first.label with "-"
    }
    if (secondNull)
    {
        //Replace second.label with "-"
    }
    if (thirdNull)
    {
        //Replace third.label with "-"
    }
}
else
{
    // Replace all labels with "-"
}

You don't need to handle every combination of the 3 fields as separate ifs...

Ideally your fields would be stored in some kind of collection (which you've omitted), that way you would be able to do something like:

if(row count == 1)
{
    foreach(field in Fields) 
    {
        if(fieldIsNull) 
        {
            // Set corresponding label to "-"
        }
    }
}

As an aside, if you always want to do a translation when reading from the database, so that if it is NULL, you want '-', then it may make sense to handle this as part of your select clause. So, with Oracle you could have:

SELECT
    NVL(FIELD1, '-'),
    NVL(FIELD2, '-'),
    NVL(FIELD3, '-')
FROM
    SOME_TABLE

I think for SQLServer, the equivalent would be:

SELECT
    ISNULL(FIELD1, '-'),
    ISNULL(FIELD2, '-'),
    ISNULL(FIELD3, '-')
FROM
    SOME_TABLE

Both of these would mean that the database returned '-' instead of NULL if the column was empty. This does of course assume that the column you're returning is a string type. If not, you would have to perform the appropriate conversion on the field to turn it into one.

Upvotes: 1

Max
Max

Reputation: 1670

Use a loop to go through each record and check whether they are null. If the record you are looking at is null, replace it with "-".

Upvotes: 0

Mihai Oprea
Mihai Oprea

Reputation: 2047

When you access the specified fields you can use the nullable operator: field ?? "-". This will yield the field name or "-" if field==null.

for example

Object field = null;
System.Console.WriteLine(field ?? "field is null");

will output:

field is null

without throwing an exception.

Upvotes: 1

Related Questions