Groups | Blog | Home
all groups > dotnet windows forms > march 2008 >

dotnet windows forms : enums bitwise values.



Ibrahim.
3/19/2008 9:18:00 AM
Hello,

I have a enum structure like this ;

[Flags]
public enum days
{
Sun=1,
Mon=2,
Tue=4,
Wed=8,
Thur=16,
Fri=32,
Sat=64
}

now i sum only sun & fri & save to database table field

int daysSelected = (int) days.sun | days.fri

now i want to get the enum values from daysSelected again ?

thanks.



Herfried K. Wagner [MVP]
3/19/2008 10:39:50 PM
"Ibrahim." <Ibrahim@discussions.microsoft.com> schrieb:
[quoted text, click to view]

=> 'days daysselected = days.sun | days.fri;'

[quoted text, click to view]

\\\
bool sunset = (daysselected & days.sun) != 0;
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
Jeff Johnson
3/24/2008 5:25:05 PM
[quoted text, click to view]

Are you saying you want to TEST the value to see if a certain day is in
there (easy, Herf gave you the code) or do you want to get the enum NAMES
back out of the value, e.g., given 33 do you want to get "Fri" and "Sun"
from it? The second part is a little more complicated, but possible.

BlackWasp
3/24/2008 9:38:18 PM
If I understand you correctly, you want to change the integer back to an
enumeration value (or combination)?

If so, just cast the integer:

days selected = (days)daysSelected;

If you output this as a string it will be comma-separated. Check out
http://www.blackwasp.co.uk/FlagsAttribute.aspx for details.

--

BlackWasp
www.blackwasp.co.uk


[quoted text, click to view]
AddThis Social Bookmark Button