checking an array of string objects for null values

dreadeddev

This is a fairly basic question I think but either my brain hasn't woken up yet or I'm just being thick!

I have a class that has a property of a collection of strings defined below (names are simplified)

public class IdentifierCollection : BaseSubCollection, IIdentifierCollection
{
    public string Id1{ get; set; }
    public string Id2{ get; set; }
    public string Id3{ get; set; }
    // ...      
}

I want to check if any of the properties actually have a value before saving so I am currently doing something like this...

if (string.IsNullOrEmpty(primaryObj.Identifiers?.Id2) &&
    string.IsNullOrEmpty(primaryObj.Identifiers?.Id2) &&
    string.IsNullOrEmpty(primaryObj.Identifiers?.Id3) &&
    string.IsNullOrEmpty(primaryObj.Identifiers?.Id4) &&
    string.IsNullOrEmpty(primaryObj.Identifiers?.Id5) &&
    string.IsNullOrEmpty(primaryObj.Identifiers?.Id6) &&
    string.IsNullOrEmpty(primaryObj.Identifiers?.Id7) &&
    string.IsNullOrEmpty(primaryObj.Identifiers?.Id8))
{

}

Just typing this feels wrong!! There must be a better way...

Kapol

I don't think there is anything wrong with this kind of property checking. Using reflection or implementing some interface just to be able to iterate over the properties looks like overkill to me. Though I agree that such a long statement looks awkward as a conditional check. To make the code more readable I'd extract this check to a separate private method.

if (NoPropIsNullOrEmpty())
{
}

private bool NoPropIsNullOrEmpty()
{
    return !(string.IsNullOrEmpty(primaryObj.Identifiers?.Id2) ||
             string.IsNullOrEmpty(primaryObj.Identifiers?.Id2) ||
             string.IsNullOrEmpty(primaryObj.Identifiers?.Id3) ||
             string.IsNullOrEmpty(primaryObj.Identifiers?.Id4) ||
             string.IsNullOrEmpty(primaryObj.Identifiers?.Id5) ||
             string.IsNullOrEmpty(primaryObj.Identifiers?.Id6) ||
             string.IsNullOrEmpty(primaryObj.Identifiers?.Id7) ||
             string.IsNullOrEmpty(primaryObj.Identifiers?.Id8));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Checking if a string contains values from a char array

Checking if string contain values from an Array in PHP

Nested objects null checking

JavaScript - Checking if all values in an array are not Null - Array / Each /

Checking for NULL values in SQL

checking null values in a dataframe

Checking db for null values

Java - Checking whether an array of objects contains a certain string

Reduce the use of foreach in array when checking null values php

compare JS objects with values null and empty string

how to delete null values in array of json objects

Filter an array of objects with null date values

Checking if an array is null or empty

Checking if first characters of a string contains values from array

javascript count objects in array of objects without null values

Filtering out objects in an array of objects without any null values

JS Iterate array of objects and replace values in string

Concatenate string values of objects in a postgresql json array

Array objects values remove unwanted string

How to put values of objects from an array into a string

mapping an array of objects and sort result by string values

Checking if a string exists in array

checking empty property for array of objects

Checking for null values in a user object

Checking for null and empty values in javascript

Checking if any values in object are not null

How to sort an array of objects by one of the objects string values numerically?

Checking to see if a String array contains all of the values of another String array (Java)

Checking for empty or null List<string>