You can use the
Value
of a nullable type only if the object is not null. If it is null, an exception is thrown. You can first check the object of a nullable type for null before trying to dereferencing it by an attempt to get the value. If the object is not null, you can get
Value
.
It could be something like this:
int? value = cb.SelectedPicklistNullableID();
if (value == null)
return -1;
else
return value.Value;
Note that "-1" (more exactly, index less then zero) selection agreement, which is always used in .NET controls with selection, makes nullable type redundant. Alternatively, you can use nullable type everywhere, taking
Value
only when you actually need to select anything using the index, or unselect or determine if something is selected, but using non-nullable indices is simple, because the agreement is already used.
—SA