0

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...

13
  • 2
    Why do you have numbered properties? Why not use a dictionary or list? You could use reflection to find and inspect the properties though, if you can't lose them. Commented Feb 25, 2016 at 10:23
  • @CodeCaster I'm wondering if that's the effect of "(names simplfied)". If it isn't, then there may well be a very different answer to the Q, ie what you're suggesting... Commented Feb 25, 2016 at 10:25
  • Yeah it is, the actual names are what I'm working on which is vaguely confidentual Commented Feb 25, 2016 at 10:26
  • But using reflection would be more expensive than just ugly code... or not? Commented Feb 25, 2016 at 10:27
  • Do you actually access these properties by name somewhere, or is a dictionary still an option? And define "expensive". Yes, reflection is "relatively slow", but it is backed by the runtime caching various objects. Commented Feb 25, 2016 at 10:27

1 Answer 1

1

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));
}
Sign up to request clarification or add additional context in comments.

2 Comments

I'm tempted to agree, the BaseCollection does implement an indexer so that I can access the properties as if they were an array, so the IEnumerable made sense but it ends up being overkill to avoid a few ugly lines of code... I've taken it out to a helper method already so I guess I'm just being too picky!!!
"I don't think there is anything wrong with this kind of property checking" - it's copy-paste code, so subject to all drawbacks thereof.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.