Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is an int translated to which enums are selected?

A previous developer (who no longer works with us, so I can't ask him) has done something pretty slick, that I really do not understand. We have this enum:

   [Flags]
    public enum SidingTypePreference
    {
        [Value(Name = "Vinyl")]
        Vinyl = 1,
        [Value(Name = "Metal or Aluminum")]
        MetalOrAluminum = 2,
        [Value(Name = "Composite")]
        Composite = 4,
        [Value(Name = "Wood")]
        Wood = 8,
        [Value(Name = "Other")]
        Other = 16
    }

In the database, SidingTypes is stored as a single int, which is a sum of all the values selected.

enter image description here

In the Model:

public SidingTypePreference? SidingTypes { get; set; }

In the controller, where table is just a row result from a query:

Model.SidingTypes = table.SidingTypes

And in the view:

@Html.EditorFor(m => Model.SidingTypes, new { @class = "form-control input-sm", GroupID = "SidingTypePreference", Cols = 1 })

But, here's the part I don't understand. Let's say that SidingTypes = 10. By some sort of voodoo magic, that int is translated into:

<input type="checkbox" class="..." name="SidingTypes_0" value="1">  Vinyl
<input type="checkbox" class="..." name="SidingTypes_1" value="2" checked>  Metal or Aluminum
<input type="checkbox" class="..." name="SidingTypes_2" value="4"> Composite
<input type="checkbox" class="..." name="SidingTypes_3" value="8" checked> Wood
<input type="checkbox" class="..." name="SidingTypes_4" value="16"> Other

(class redacted just to prevent the need to scroll, but they are all 'lookup-checkbox-SidingTypes'.)

From the value of that int, it knows which are checked and which are not.

FIRST QUESTION: Is this native .NET? Or is there some extension method or template that I need to find?

SECOND QUESTION: What I need to do, independent of anything else, is to build a method to determine if an enum is selected.

Something like:

private bool IsSelected(int SidingTypes, SidingTypePreference sidingTypePreference)
{
  ... ?? ...
  return true or false;
}
like image 877
Casey Crookston Avatar asked Dec 05 '25 05:12

Casey Crookston


2 Answers

Is this native .NET?

Part of it is. Enums with the Flags attribute decompose a value into the equivalent bits that would make up that composite value (e.g. 10 = 2 + 8)

Converting that value into a set of checkboxes is not native. I suspect there is a custom template in the project somewhere to convert the enum value into a set of checkboxes

How do I determine if an enum is selected.

Just use bitwise operations:

private bool IsSelected(int SidingTypes, SidingTypePreference sidingTypePreference)
{
  return (SidingTypes & (int)sidingTypePreference) != 0;
}

Since the & operator will return a number with the bits that are "selected" in both SidingTypes and sidingTypePreference. If no flags match, the result will be all zeros; so any number other than 0 indicates a match.

like image 173
D Stanley Avatar answered Dec 07 '25 21:12

D Stanley


Last I checked (a couple of years ago now), ASP.NET MVC didn't have a built-in template for flags enums like this. However, it's likely that the developer created a custom editor template to recognize this type (or maybe flag enums generally) and display an editor for it.

Check out the files in Views/Shared/EditorTemplates/--particularly ones named SidingTypePreference.cshtml or Object.cshtml. It might also help to do a full-text search for lookup-checkbox- anywhere in your project.

For your second question, D Stanley's answer is correct: you use a bitwise & operator against the value that you're interested in, and then check to see if the result is non-zero.

like image 36
StriplingWarrior Avatar answered Dec 07 '25 21:12

StriplingWarrior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!