C# Enum Flag - two ways binding - enum - class -enum -


i have following enum

[flags] public enum weekdays {     monday = 1,     tuesday = 2,     wednesday = 4,     thursday = 8,     friday = 16,     saturday = 32,     sunday = 64 } 

in ui, user can select ceratain days: monday, tuesday, wednesday example. user selection of monday, tuesday, wednesday 7. values saved in databse in column called days.

now if have class:

public class week {     public bool monday { get; set; }     public bool tuesday { get; set; }     public bool wednesday { get; set; }     public bool thursday { get; set; }     public bool friday { get; set; }     public bool saturday { get; set; }     public bool sunday { get; set; } } 

how can bind value 7 , make appropriate properties true or false. example: 7 equivalent monday, tuesday, wednesday enum. if convert value 7 class week, result properties: monday, tuesday, wednesday true, , rest false.

if instead have class week properties: monday, tuesday, wednesday true, , convert enum weekdays result 7.

how can that?

you make week have property or field of type weekdays keeps track of flags active. , boolean properties check against enum value, , on set update correctly. allows this:

week w = new week(); w.monday = true; console.writeline(w.days); // monday w.tuesday = true; w.wednesday = true; console.writeline(w.days); // monday, tuesday, wednesday 

see week code below, quite verbose (albeit introducing setdaysflag helper method):

public class week {     public weekdays days     { get; set; }      public bool monday     {         { return (days & weekdays.monday) != 0; }         set { setdaysflag(weekdays.monday, value); }     }      public bool tuesday     {         { return (days & weekdays.tuesday) != 0; }         set { setdaysflag(weekdays.tuesday, value); }     }      public bool wednesday     {         { return (days & weekdays.wednesday) != 0; }         set { setdaysflag(weekdays.wednesday, value); }     }      public bool thursday     {         { return (days & weekdays.thursday) != 0; }         set { setdaysflag(weekdays.thursday, value); }     }      public bool friday     {         { return (days & weekdays.friday) != 0; }         set { setdaysflag(weekdays.friday, value); }     }      public bool saturday     {         { return (days & weekdays.saturday) != 0; }         set { setdaysflag(weekdays.saturday, value); }     }      public bool sunday     {         { return (days & weekdays.sunday) != 0; }         set { setdaysflag(weekdays.sunday, value); }     }      /// <summary>     /// set or unset flag on <c>days</c> property.     /// </summary>     /// <param name="flag">the flag set or unset.</param>     /// <param name="state">true when flag should set, or false when should removed.</param>     private void setdaysflag (weekdays flag, bool state)     {         if (state)             days |= flag;         else             days &= ~flag;     } } 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -