Arie
Arie

Reputation: 3573

Condition for list count and null

I have following constructor as shown below:

public Delivery(DeliveryPeriodEnum deliveryPeriod, IEnumerable<DayOfWeek> days)
{
       _deliveryPeriod = deliveryPeriod;
       _days = days;

        if (_deliveryPeriod == DeliveryPeriodEnum.Nothing 
            && (_days != null || _days.Any())) 
            throw new GeneralException("There cannot be days for given period");

        if (_deliveryPeriod != DeliveryPeriodEnum.Nothing 
            && (_days == null || !_days.Any())) 
            throw new GeneralException("Period has to have at elast one item in list");
}

There are two business conditions:

//if DeliveryPeriodEnum.Nothing:
then days have to be either null or not null but with count = 0 otherwise show message

//if DeliveryPeriodEnum <> DeliveryPeriodEnum.Nothing:
then days cannot be null and have to have count > 0 otherwise show message

I have problems with it for instance in my first if statment if DeliveryPeriodEnum = DeliveryPeriodEnum .Nothing and days is null it also evalueates to _days.Any() which certainly raise error of instance not exist.

Keep in mind i would like to have both conditions in two lines if possible to avoid big if else statments etc..

EDIT:

if (_deliveryPeriod == DeliveryPeriodEnum.Nothing 
    && _days != null && _days.Any()) 
    throw new GenericException("There cannot be days for given period");
if ((_deliveryPeriod != DeliveryPeriodEnum.Nothing && _days != null && !_days.Any())
    throw new GenericException("There cannot be days for given period");

Upvotes: 2

Views: 304

Answers (3)

ZiNNED
ZiNNED

Reputation: 2650

Just use the null-conditional operator like below. When _days is null the comparison leads to false in the first comparison and true in the second one. When it's not null, it just evaluates the .Any() statement.

public Delivery(DeliveryPeriodEnum deliveryPeriod, IEnumerable<DayOfWeek> days)
{
       _deliveryPeriod = deliveryPeriod;
       _days = days;

        if (_deliveryPeriod == DeliveryPeriodEnum.Nothing && _days?.Any() == true)
            throw new GeneralException("There cannot be days for given period");

        if (_deliveryPeriod != DeliveryPeriodEnum.Nothing && (_days?.Any() ?? false) == false)
            throw new GeneralException("Period has to have at least one item in list");
}

Upvotes: 1

StepUp
StepUp

Reputation: 38154

Try to check by && not || as this condition can throw error _days != null || _days.Any()). Because _days can be null:

if (_deliveryPeriod == DeliveryPeriodEnum.Nothing 
    && (_days != null && _days.Any())) 
    throw new GeneralException("There cannot be days for given period");

if (_deliveryPeriod != DeliveryPeriodEnum.Nothing 
    && ((_days != null && !_days.Any()) || _days == null)) 
    throw new GeneralException("Period has to have at elast one item in list");

Upvotes: 2

Giorgos Betsos
Giorgos Betsos

Reputation: 72185

First condition:

if DeliveryPeriodEnum.Nothing: then days have to be either null or not null but with count = 0 otherwise show message

 if (!((_deliveryPeriod == DeliveryPeriodEnum.Nothing) && (_days == null || !_days.Any()))) 
     throw new GeneralException("There cannot be days for given period");

Second condition:

if DeliveryPeriodEnum <> DeliveryPeriodEnum.Nothing: then days cannot be null and have to have count > 0 otherwise show message

if (!((_deliveryPeriod != DeliveryPeriodEnum.Nothing) && (_days != null) && _days.Any())))
    throw new GeneralException("Period has to have at elast one item in list");

Upvotes: 1

Related Questions