2

What is the best way to do this when looping a list? I have tried to evaluate it with something like this but it wont let me evaluate bool as an "if" condition

   if (item["ShowTitle"] == true)
//do something
5

You can use:

 if (bool.Parse(item["ShowTitle"].ToString()) == true)

or

if (item["ShowTitle"].ToString() == true.ToString())
3

All answers are not safe, when the field is null. This can happen, when a field gets later added and already items are in this list.

This is a safe cast:

bool? showTitle = properties.ListItem["ShowTitle"] as bool?;
if (showTitle.HasValue && showTitle.Value)
{

}
  • I really like your approach and would love to have this the accepted answer! – Andreas Nov 13 '17 at 10:35
2

I think this would be the cleanest way of doing it with C#:

    if ((bool)item["ShowTitle"] == true)
    {
        //Do stuff
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.