How can I convert from a bool to an int?

  • A+

My code looks like this:

if (Settings.adp == true)    App.DB.UpdateIntSetting(SET.Adp, 0); else    App.DB.UpdateIntSetting(SET.Adp, 1); 

Is there a way I could get this down to one line by somehow converting the bool Settings.adp into an int (0 or 1)


You can use the ternary operator to consolidate it to one line:

App.DB.UpdateIntSetting(SET.Adp, Settings.adp ? 0 : 1); 

From the documentation

condition ? consequence : alternative


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: