I am currently working with some automated jobs sheduled from an SQL-Database.
Each Job as a Column 'RunDays'
of type string(nvarchar(7))
.
Let's say the value is '1111111'
that means 'SMTWTFS'
(Sunday,Monday,[...]).
I've created this enum:
public enum RunDays : int
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thrusday = 4,
Friday = 5,
Saturday = 6
//Maybe int is bad here cause Wednesday = 3 & Monday + Tuesday is also 3 -.-
}
But how to convert to that and vice versa ?
Here's what I tried so far:
if(row["RunDays"] != DBNull.Value)
{
for(int i = 0; i < row["RunDays"].ToString().Length; i++)
{
if(int.Parse(row["RunDays"].ToString()[i].ToString()) == 1)
{
job.RunDays = job.RunDays | (RunDays)i;
}
}
}
Sadly all I am getting is 'Sunday'
as the value.
What am I doing wrong/ what am I missing ?
If you want multiple enum values you need an enum marked with the
[Flags]
attribute and give each member a value that is a power of 2:The
[Flags]
attribute enables the enum to take values that are not explicitly declared (e.g. 5) and the powers of 2 ensure that each combination is unique.