Any trick to defining an enum as flags/powers of 2 without eventually needing a calculator?
Write the values as shifted bits and let the compiler do the math:
[Flags]
private enum Targets : uint
{
None = 0,
Campaigns = 1,
CampaignGroups = 2 << 0,
Advertisers = 2 << 1,
AdvertiserGroups = 2 << 2,
AffiliateGroups = 2 << 3,
Affiliates = 2 << 4,
Creatives = 2 << 5,
DetailedLeads = 2 << 6,
DetailedSales = 2 << 7,
ProgramLeads = 2 << 8,
CreativeDeployments = 2 << 9,
CampaignCategories = 2 << 10,
Payouts = 2 << 11,
// etc.
}
James's suggestion is a good one, too. In fact I like this way even better. You could also write it like this:
[Flags]
private enum Targets : uint
{
None = 0,
Campaigns = 1 << 0,
CampaignGroups = 1 << 1,
Advertisers = 1 << 2,
AdvertiserGroups = 1 << 3,
AffiliateGroups = 1 << 4,
// etc.
}
Using hexadecimal notation is a little simpler than decimal notation as well (no calculator required):
[Flags]
private enum Targets : uint
{
None = 0,
Campaigns = 0x01,
CampaignGroups = 0x02,
Advertisers = 0x04,
AdvertiserGroups = 0x08,
AffiliateGroups = 0x10,
Affiliates = 0x20,
Creatives = 0x40,
DetailedLeads = 0x80,
DetailedSales = 0x100,
ProgramLeads = 0x200,
CreativeDeployments = 0x400,
CampaignCategories = 0x800,
Payouts = 0x1000,
// and the pattern of doubling continues
// 0x2000
// 0x4000
// 0x8000
// 0x10000
}
Not quite as elegant as Cody and James' solutions, but requires no calculator.
Fast forward five years into the future, and starting with C# 7.0 you can use the new numeric binary literal to simplify the enum flags declaration.
[Flags]
private enum Targets : uint
{
None = 0,
Campaigns = 0b0000_0000_0000_0001,
CampaignGroups = 0b0000_0000_0000_0010,
Advertisers = 0b0000_0000_0000_0100,
AdvertiserGroups = 0b0000_0000_0000_1000,
AffiliateGroups = 0b0000_0000_0001_0000,
Affiliates = 0b0000_0000_0010_0000,
Creatives = 0b0000_0000_0100_0000,
DetailedLeads = 0b0000_0000_1000_0000,
DetailedSales = 0b0000_0001_0000_0000,
ProgramLeads = 0b0000_0010_0000_0000,
CreativeDeployments = 0b0000_0100_0000_0000,
CampaignCategories = 0b0000_1000_0000_0000,
Payouts = 0b0001_0000_0000_0000,
All = uint.MaxValue
}
https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-7#numeric-literal-syntax-improvements