vinmm
vinmm

Reputation: 297

Adding two nullable integers not working

I am having a very strange problem. Problem looks funny and simple but its making me mad.

I have a nullable integer in a class which are declared as

public int? count { get; set; }

I have an array of objects(additionalCell) of this class and another object of the same class called currentPixelTotalCell. I want to add values of the count variable of all the objects in the array and store it in the count variable of currentPixelTotalCell.

My code is as below. But when debugging, i see that the left hand part has value as null only after the loop is exited although count variables in all the object have non-null value.

for(int i = 0; i < 5; i++)
{    
    currentPixelTotalCell.count += additionalCell[i].count;
}

Any idea why is this happening ? Is there a different way to add them ? I am clueless.

Edit:

Forgot to mentioned this. When I have breakpoint and check in first iteration itself, it doesnt add up. Eg. If additionalCell[0].count was 10. Then value of currentPixelTotalCell.count used to be null only even after the inner line was executed in first iteration.

Upvotes: 1

Views: 1199

Answers (6)

Stefan Steinegger
Stefan Steinegger

Reputation: 64628

I guess the result is null because one of the values is null.

What about:

 currentPixelTotalCell.count += additionalCell.Select(x => x.count)
                                              .Where(x => x.HasValue)
                                              .Sum();

Or

 currentPixelTotalCell.count += additionalCell.Sum(x => x.count ?? 0);

Don't forget to initialize currentPixelTotalCell.count somewhere or replace += by a simple assignment =.

Upvotes: 1

Gilles
Gilles

Reputation: 95

Could it be that you need to initialize the currentPixelTotalCell.count variable to 0 first?

currentPixelTotalCell.count = 0;
for(int i = 0; i < 5; i++)
{    
    currentPixelTotalCell.count += additionalCell[i].count;
}

or you may have to check for null values in AdditionalCell objects?

for(int i = 0; i < 5; i++)
{    
    currentPixelTotalCell.count += (additionalCell[i].count ?? 0)
}

Upvotes: 3

Pablo
Pablo

Reputation: 75

You need to initialise currentPixelTotalCell.count to 0 before accesing it.

Remember that "a += b" is just syntactic sugar for "a = a + b".

Because a is null, you are effectively doing "a = null + b" and null plus something equals null.

Also because of the same constraint, you need to ensure the value on the right hand side is also not null. The easier way to do this in your case is to simply use the GetValueOrDefault method.

All this being said, your final solution should be something like:

currentPixelTotalCell.count = 0;
for(int i = 0; i < 5; i++)
{    
    currentPixelTotalCell.count += additionalCell[i].count.GetValueOrDefault();
}

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186698

If you have null as the result then

  • Either currentPixelTotalCell.count is null
  • Some of additionalCell[i].count is null

Ensure that both null are under control

   // currentPixelTotalCell.count is not null
   currentPixelTotalCell.count = 0;

   for(int i = 0; i < 5; i++)
   {    
       // if additionalCell[i].count is null treat it as 0
       currentPixelTotalCell.count += additionalCell[i].count ?? 0;
   }

you can try Linq as an alternative:

   currentPixelTotalCell.count = additionalCell
     .Take(5) // if you want take just first 5 items
     .Sum(item => item ?? 0);

Upvotes: 2

sujith karivelil
sujith karivelil

Reputation: 29026

There is a method called .GetValueOrDefault() which will give you the default value of the Nullable<T>. You can make use of it to assign 0 if the value is null:

for(int i = 0; i < 5; i++)
{    
    currentPixelTotalCell.CmvF =currentPixelTotalCell.CmvF.GetValueOrDefault() +  additionalCell[i].CmvF.GetValueOrDefault();
}

Upvotes: 0

Saverio Terracciano
Saverio Terracciano

Reputation: 3915

Change your inner loop to:

currentPixelTotalCell.count += (additionalCell[i].count ?? 0);

to avoid setting your total to null in case one of the right handed values is null.

Upvotes: 2

Related Questions