Getting value from SPFieldBoolean

Christian P. picture Christian P. · Oct 27, 2010 · Viewed 7k times · Source

How do I get the value from a SPFieldBoolean object? Do I simply cast it to a boolean or do I need to do something further with it?

I am fetching it in an EventReceiver class during an ItemAdded event from properties.ListItem["fieldname"].

If there is a chance the field might not exist (and be null), how do I check for that?

Answer

Ondrej Tucny picture Ondrej Tucny · Oct 27, 2010

The value is already a bool, you just need to type-cast it. All fields provide values in their native value-type — see also SPField.FieldValueType property that gives you the actual type in case you need to inspect it on runtime.

To make sure the field is contained in the list, just use the SPFieldCollection.ContainsField method on your list's Fields collection.