posted on Wednesday, June 25, 2008 8:23 PM by BayerWhite

Generic "HasValue" And Policy Activity Gotcha

Recently I noticed this error, Cannot evaluate property "HasValue" because its target object is null. I did not get it at compilation of my workflow project, but running a workflow application. I use generic data types whenever possible, and I use the "HasValue" property as well, so when I started getting this error I was not sure where to start looking. Fortunately, this error means exactly what it says and since I cannot debug to the point where it occurs it probably means it is happening within a WF rule or condition.

Normally if you have code that looks like if(NewPerson.IsMarried.HasValue && NewPerson.IsMarried.Value == true) it will process fine, however if you use generic data types regularly like me within policy activities you will come across the same error message like above. Fortunately, the way around this issue is to simply use the null condition so that your policy's business rules look similiar to this... if(NewPerson.IsMarried!=null && NewPerson.IsMarried.Value == true)

Comments