snippetcsharpMinor
Parse Flags enum from array of booleans
Viewed 0 times
arrayenumparseflagsbooleansfrom
Problem
I need to get from an array of booleans to a Flags enum.
Here's what my enum looks like:
Here's what my parsing code looks like currently.
That's horrible! I must be able to do better. Any ideas?
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?
Solution
Well, you need some sort of mapping between the parameters and the
If you had more parameters (though that would be a code smell), it might be worth considering something like using a dictionary. But I'm not sure it's a good idea, it's certainly longer than the previous version:
enum values, so you can't actually avoid having three almost same pieces of code. But you can make your code shorter by using ifs and |= instead of ternary operators:private UserContactPreferences GetContactPreferences(bool email, bool phone, bool sms)
{
var preferences = UserContactPreferences.None;
if (email)
preferences |= UserContactPreferences.Email;
if (phone)
preferences |= UserContactPreferences.Phone;
if (sms)
preferences |= UserContactPreferences.SMS;
return preferences;
}If you had more parameters (though that would be a code smell), it might be worth considering something like using a dictionary. But I'm not sure it's a good idea, it's certainly longer than the previous version:
private UserContactPreferences GetContactPreferences(bool email, bool phone, bool sms)
{
var dictionary = new Dictionary
{
{ UserContactPreferences.Email, email },
{ UserContactPreferences.Phone, phone },
{ UserContactPreferences.SMS, sms }
};
var preferences = UserContactPreferences.None;
foreach (var kvp in dictionary)
{
if (kvp.Value)
preferences |= kvp.Key;
}
return preferences;
}Code Snippets
private UserContactPreferences GetContactPreferences(bool email, bool phone, bool sms)
{
var preferences = UserContactPreferences.None;
if (email)
preferences |= UserContactPreferences.Email;
if (phone)
preferences |= UserContactPreferences.Phone;
if (sms)
preferences |= UserContactPreferences.SMS;
return preferences;
}private UserContactPreferences GetContactPreferences(bool email, bool phone, bool sms)
{
var dictionary = new Dictionary<UserContactPreferences, bool>
{
{ UserContactPreferences.Email, email },
{ UserContactPreferences.Phone, phone },
{ UserContactPreferences.SMS, sms }
};
var preferences = UserContactPreferences.None;
foreach (var kvp in dictionary)
{
if (kvp.Value)
preferences |= kvp.Key;
}
return preferences;
}Context
StackExchange Code Review Q#21331, answer score: 9
Revisions (0)
No revisions yet.