2

I have the following enumerator defined:

[Flags]
public enum CoinSizes
{
    [Display(Name = "0.01")]
    OneCent = 0x1,
    [Display(Name = "0.02")]
    TwoCent = 0x2,
    [Display(Name = "0.05")]
    FiveCent = 0x4,
    [Display(Name = "0.10")]
    TenCent = 0x8,
    [Display(Name = "0.20")]
    TwentyCent = 0x16,
    [Display(Name = "0.25")]
    TwentyFiveCent = 0x32,
    [Display(Name = "0.50")]
    FiftyCent = 0x64,
    [Display(Name = "1.00")]
    OneDollar = 0x128,
    [Display(Name = "2.00")]
    TowDollar = 0x256,
    [Display(Name = "5.00")]
    FiveDollar = 0x512,
    [Display(Name = "10.00")]
    TenDollar = 0x1024,
    [Display(Name = "25.00")]
    TwentyFiveDollar = 0x2048,
    [Display(Name = "50.00")]
    FiftyDollar = 0x4096
}

I'm building an MVC web app and I have a custom EnumCheckboxListFor helper defined. In the helper, I iterate over the Enum items with Linq and check which ones are selected and therefor which checkboxes need to be checked.

I'm using the following to get the full list of enumerations available, which are then iterated over:

Enum.GetValues((typeof(TEnum)))

LINQ:

TEnum enumValues = expression.Compile()((TModel)htmlHelper.ViewContext.ViewData.Model);
    IEnumerable<SelectListItem> items = Enum
        .GetValues(enumType)
        .Cast<Enum>()
        .Select(c => new SelectListItem
        {
            Text = c.EnumDisplayName().ToString(),
            Value = c.ToString(),
            Selected = enumValues != null && (value.Equals(c) || value.ToString().Contains(c.ToString()))
        }).ToList();

I know the logic to determine "Selected" isn't perfect, but that's a WIP. Mainly, the array that Enum.GetValues() returns is odd:

Enum.GetValues((typeof(TEnum)))
    {Href.DataStore.Gambling.Core.CoinSizes[13]}
        [0]: OneCent
        [1]: TwoCent
        [2]: FiveCent
        [3]: TenCent
        [4]: 22
        [5]: 50
        [6]: 100
        [7]: 296
        [8]: 598
        [9]: 1298
        [10]: 4132
        [11]: 8264
        [12]: 16534

The first four are correct, but obviously those int values (from index 4 onward) don't correspond to the enumerations' values.

Any ideas why I'd be getting those odd values?

Adam Hey
  • 1,512
  • 1
  • 20
  • 24
  • `GetValues()` should return only values 1, 2, 4, 8, 22 etc. If you cast value to enum and get it's string representation you will have enum member names. You should not have mixed results here. Are you showing us `Value` of selectlistitem, or it's `Text`? – Sergey Berezovskiy Feb 18 '16 at 08:52
  • 1
    I think you are misunderstanding hexadecimal... – Dennis_E Feb 18 '16 at 08:54
  • 3
    the int values are correct, they are just the decimal values of the hex numbers. The first four give the correct result because the hex and decimal are the same value. – secret squirrel Feb 18 '16 at 08:57
  • @secretsquirrel you're spot on! I changed the hex values to ints and now the returned array shows the enum values as you'd expect – Adam Hey Feb 18 '16 at 09:10

2 Answers2

2

0X16 is a hexadecimal value which is equal to 1*16 + 6 = 22.

Similarly,

0x32 = 3*16 + 2 = 50

0x64 = 6*16 + 4 = 100 and so on.

Try changing hexadecimal values to decimal ones, i.e,

[Flags]
public enum CoinSizes
{
    [Display(Name = "0.01")]
    OneCent = 1,
    [Display(Name = "0.02")]
    TwoCent = 2,
    [Display(Name = "0.05")]
    FiveCent = 4,
    [Display(Name = "0.10")]
    TenCent = 8,
    [Display(Name = "0.20")]
    TwentyCent = 16,
    [Display(Name = "0.25")]
    TwentyFiveCent = 32,
    [Display(Name = "0.50")]
    FiftyCent = 64,
    [Display(Name = "1.00")]
    OneDollar = 128,
    [Display(Name = "2.00")]
    TowDollar = 256,
    [Display(Name = "5.00")]
    FiveDollar = 512,
    [Display(Name = "10.00")]
    TenDollar = 1024,
    [Display(Name = "25.00")]
    TwentyFiveDollar = 2048,
    [Display(Name = "50.00")]
    FiftyDollar = 4096
}

It would be unwise to use Flags enums with colliding values. If used, Oring or ANDing them would produce undesired results.

Community
  • 1
  • 1
tafa
  • 7,146
  • 3
  • 36
  • 40
1

The returned values are correct you use hex code. So 0x16 is 22. Because 1*16+6 is 22. Also for the other values like 0x64 is 100.

Zelldon
  • 5,396
  • 3
  • 34
  • 46