Enum question

B

Bill Gower

I have a group of 7 checkboxes representing the days of the week. I have an
enum that that corresponds to the days ex. DaysOfWeek.Sunday,
DaysOfWeek.Monday etc. I have a library that has a function that I call and
need to pass the days of the week that the user has selected concatenated by
a |.

What is the easiest and cleanest way to go through all the checkboxes and if
the value is true concatenate the enum values together to pass to the
function?

Bill
 
P

Peter Duniho

[...]
What is the easiest and cleanest way to go through all the checkboxes
and if
the value is true concatenate the enum values together to pass to the
function?

I will refrain from making absolute claims of "easiest" or "cleanest", but
it seems to me that you could take advantage of the Tag property of the
controls and group them together with a group box. Then your code would
just enumerate all of the controls in the group box, parsing each one's
Tag if it's checked, and or-ing that into your enumeration.

For example:

MyEnum myenum = MyEnum.None;

foreach (CheckBox chk in groupBox1.Controls)
{
if (chk.Checked)
{
myenum |= (MyEnum)Enum.Parse(typeof(MyEnum),
chk.Tag.ToString());
}
}

If the Name or Text property of the checkboxes just happened to coincide
with the names of the enum values, you could even just use either of those
properties instead as appropriate.

Pete
 
D

Diego Jancic

Hi,
If you can modify the function that receives the days in a string, I
suggest use the enum as a binary flag, for example:

[Flags]
enum Days
{
Monday = 1,
Tuesday = 2,
Wednesday = 4,
Thursday = 8,
Friday = 16,
Saturday = 32
}

So you can manage it better using bitwise operators, to read all the
selected values:

Days d;
if (chkMonday.Checked)
d |= Days.Monday;
if (chkTuesday.Checked)
d |= Days.Tuesday;
....

To check whether the user choosen a day, you can simply test without
using substrings:

if (selectedDay & Days.Sunday == Days.Sunday)


Anyway, if you can't change that function, I would do it this way:

CheckBox[] chks = new CheckBox[] { chkMonday, chkTuesday,
chkWednesday, ... }
StringBuilder days = new ...();
for (int d = 0 ; d < 7 /* or chks.Length */ ; d++)
{
if (d != 0)
days.Append("|");
days.Append(d);
}


Hope this helps,
Diego
 
P

PS

Bill Gower said:
I have a group of 7 checkboxes representing the days of the week. I have
an enum that that corresponds to the days ex. DaysOfWeek.Sunday,
DaysOfWeek.Monday etc. I have a library that has a function that I call
and need to pass the days of the week that the user has selected
concatenated by a |.

What is the easiest and cleanest way to go through all the checkboxes and
if the value is true concatenate the enum values together to pass to the
function?

If you are going to be dealing with with enum a lot then I would have an
utility class that takes boolean values, which you can pass the checkbox
Checked property.

public static DaysOfWeek Build(
bool sunday,
bool monday,
bool tuesday,
bool wednesday,
bool thursday,
bool friday,
bool saturday)
{
DaysOfWeek daysOfWeek = new DaysOfWeek();
if(sunday) daysOfWeek = daysOfWeek | DaysOfWeek.Sunday;
if(monday) daysOfWeek = daysOfWeek | DaysOfWeek.Monday;
........
return DaysOfWeek;
}

PS
 
C

Christof Nordiek

[...]
What is the easiest and cleanest way to go through all the checkboxes
and if
the value is true concatenate the enum values together to pass to the
function?
For example:

MyEnum myenum = MyEnum.None;

foreach (CheckBox chk in groupBox1.Controls)
{
if (chk.Checked)
{
myenum |= (MyEnum)Enum.Parse(typeof(MyEnum),
chk.Tag.ToString());
}
}

It would be easier, to assign the Enum-values to the Tag properties.

Christof
 
B

Bob Powell [MVP]

It would be easier for you if the enumeration declaration is tagged with the
Flags attribute.
Then the enum would be or-able.

You can create your text boxes in any of the ways suggested by other posters
and then pass a single value containing the sum of the selected days.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top