Arokh Posted February 5, 2009 Posted February 5, 2009 Hi, I've created an enum with the Flag attribute and I want to use it as such, but the creating part is a little annoying. I've tried to make it less annoying by using Math.Pow but Csharp doesn't like that: [CSharp][FlagsAttribute] public enum eDBFCode { //Byte 1 //B17=Math.Pow(2,0), B16_AId = Math.Pow(2, 1), B15_EId = Math.Pow(2, 2), B14_GId = Math.Pow(2, 3), B13_LId = Math.Pow(2, 4), B12_LstOtherEps = Math.Pow(2, 5), B11_IsDeprecated = Math.Pow(2, 6), B10_State = Math.Pow(2, 7), //Byte 2 B27_Size = Math.Pow(2, 8), B26_Ed2k = Math.Pow(2, 9), B25_Md5 = Math.Pow(2, 10), B24_Sha1 = Math.Pow(2, 11), B23_Crc32 = Math.Pow(2, 12), //B22=Math.Pow(2,13), //B21=Math.Pow(2,14), //B20=Math.Pow(2,15), //Byte 3 B37_Quality = Math.Pow(2, 16), B36_Source = Math.Pow(2, 17), B35_AudioCodec = Math.Pow(2, 18), B34_AudioBitRate = Math.Pow(2, 19), B33_VideoCodec = Math.Pow(2, 20), B32_VideoBitRate = Math.Pow(2, 21), B31_VideoRes = Math.Pow(2, 22), B30_FileExt = Math.Pow(2, 23), //Byte 4 B47_DubLanguage = Math.Pow(2, 24), B46_SubLanguage = Math.Pow(2, 25), B45_LengthInSec = Math.Pow(2, 26), B44_Description = Math.Pow(2, 27), B43_ReleaseDate = Math.Pow(2, 28), //B42=Math.Pow(2,29), //B41=Math.Pow(2,30), B40_AniDBFileName = Math.Pow(2, 31) } [/CSharp] Is there a way of 'automatically' setting the right values for the enum? Entering every value manually isn't something I'm too fond of. Quote
Arokh Posted February 5, 2009 Author Posted February 5, 2009 I found a nice solution, with bitwise shifting. B33_VideoCodec = 1048576 vs B33_VideoCodec = 1 << 20 But if there is still a simpler solution let me hear it. Quote
Leaders snarfblam Posted February 5, 2009 Leaders Posted February 5, 2009 Unfortunately, C# has no built-in support for [Flags] enumerations. In other words, no automatic value assignment, no direct access to individual flags in composite values, etc.. I prefer to write hex literals for the constants, but your bit-shifting solution is probably your best bet. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.