473,513 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Enum question

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
Jun 26 '07 #1
5 5103
On Mon, 25 Jun 2007 18:07:56 -0700, Bill Gower <bi*******@charter.net
wrote:
[...]
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
Jun 26 '07 #2
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

Jun 26 '07 #3
PS

"Bill Gower" <bi*******@charter.netwrote in message
news:OC*************@TK2MSFTNGP05.phx.gbl...
>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
>
Bill

Jun 26 '07 #4
"Peter Duniho" <Np*********@nnowslpianmk.comschrieb im Newsbeitrag
news:op***************@petes-computer.local...
On Mon, 25 Jun 2007 18:07:56 -0700, Bill Gower <bi*******@charter.net>
wrote:
>[...]
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
Jun 26 '07 #5
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.
"Bill Gower" <bi*******@charter.netwrote in message
news:OC*************@TK2MSFTNGP05.phx.gbl...
>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
Jun 30 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
4796
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
10
5768
by: James Brown | last post by:
I have the following enum declared: enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING }; (it goes on and on like that) This is what I would like to do: TOKEN t1 = TOK_ID; // ok...
4
6469
by: Nikhil Patel | last post by:
Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent?...
5
2653
by: Andrea Williams | last post by:
I'm working with C# and I'm setting up some ENUM's I have a data and Business layer. I'm declaring a common enum for the Data Layer. The UI layer references the Bus layer and the bus layer...
6
2859
by: Michael Isaacs | last post by:
Regarding use of enum's, I am wondering what the cost of memory is when creating the enumeration on the calling side, and then using it on the function/method side. See example below. If I...
2
3390
by: Dennis | last post by:
I have an enum as follows: Public Enum myData FirstData = 6 SecondData = 7 end enum Is there anyway that I can return the Enum names by their value, i.e., I want to input 6 into a function...
13
12361
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
3
3892
by: K. Wilder | last post by:
I need to declare a project level Enum that any procedure in any class can reference so there's continuity with the values. How do I do this? At present, if I declare a Module in a project and...
34
11133
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
4
1932
by: ice8595 | last post by:
Hi there, I'm fairly new at this, and I am having a bit of trouble wrapping my head around some concepts of enum for a project. So any help would be greatly appreciated. Essentially I'm...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7523
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.