473,326 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 5097
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
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
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
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
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
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
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
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
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
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
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.