Blog Stats
  • Posts - 62
  • Articles - 1
  • Comments - 41
  • Trackbacks - 63

 

Nullable AhA!'s

Say you have a property like this:
private int? _startOdometer;
public int? StartOdometer
{
get
{
if (_startOdometer.HasValue) { return _startOdometer.Value; }
return null;
}
set
{
_startOdometer = value;
}
}

When you try to do the following what will the value of _startOdometer be?
myObject.StartOdometer += myIntegerValue;
ANSWER: NULL
In order to do this kind of operator on a nullable int (or any other nullable value), you have to do:
myObject.StartOdometer = myObject.GetDefaultValueOrValue() + myIntegerValue;
AHA!

Feedback

No comments posted yet.


Post a comment





 

 

 

Copyright © Mike Nichols