I need to get from an array of booleans to a Flags enum.
Here's what my enum looks like:
[Flags]
public enum UserContactPreferences { None = 0, SMS = 1, Email = 2, Phone = 4 }
Here's what my parsing code looks like currently.
private UserContactPreferences GetContactPreferences(bool email, bool phone, bool sms)
{
var contactByEmail = email ? UserContactPreferences.Email : UserContactPreferences.None;
var contactByPhone = phone ? UserContactPreferences.Phone : UserContactPreferences.None;
var contactBySms = sms ? UserContactPreferences.SMS : UserContactPreferences.None;
return contactByEmail | contactByPhone | contactBySms;
}
That's horrible! I must be able to do better. Any ideas?