Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I'm looking to declare an enum in a separate class called "workhours" and then use a checked list to allow selection of these days.

I'm then using a "calculate total hours" button to total up the result in a label.

Here are some screenshots:

Enum - Album on Imgur[^]

My problem is that I get an exception unhandled and i'm not sure how to calculate total hours from the drop down list.

Thanks.

What I have tried:

I have tried creating a function called
InitializeCheckedComboBox();
and calling the enum like this
checkedComboBox_WorkHours.Properties.Items.AddRange(Enum.GetNames(typeof(Workhours)));
Posted
Updated 5-Aug-21 3:41am
Comments
Richard MacCutchan 4-Aug-21 4:39am    
You have two definitions of WorkHours, a class and an enum.
Will Sewell 4-Aug-21 5:19am    
Thank you.
BillWoodruff 4-Aug-21 11:14am    
Do you have a solution now, or would you like some help ? There's a much simpler way to get what you want without using an Enum.

Of course, Enums are useful for lots of things, but, in terms of binding to controls, and using the whatever/results the user does with those controls, there are good reasons not to use them, which I will explain, if you wish,
Will Sewell 5-Aug-21 5:00am    
I would still like some help please.

Quote:
My problem is that I get an exception unhandled

And the problem we have is that we have no idea what exception, or where - we can see two lines of code that may or may not be related, and a image of an enum (instead of teh actual code which would have helped more). Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.

As a result, in reality we can't help you at all!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
Will Sewell 4-Aug-21 4:32am    
Thanks for your response. Did you click on the link to the imgur album with screenshots of my code? I'm not sure if there is a way to post the screenshots in the post other than doing it this way.
OriginalGriff 4-Aug-21 5:14am    
I'm not interested in screenshots - they don't enable anyone to test your code without retyping it all.
You want to show code, copy'n'paste it!

What has the debugger shown you so far?
Will Sewell 4-Aug-21 5:19am    
Ah I see. I'll not do those in future. Maybe only of the exception itself. My problem was with naming the class the same thing as the enum. Thanks for your help.
First, realize that the CheckedListbox is kind of an old fossil Control that does not offer the binding goodness of newer controls: [^]. You may find that setting the DataSource of a CheckedListbox in a standard way suddenly throws errors: [^]

You need to use AddRange to set the Items in the CheckedListbox: AddRange requires ab Array of Object. Example:
dictStrToInt = new Dictionary<string, int>()
{
    { "M", 5 },
    { "Tu", 10 },
    { "W", 15 },
    {"Th", 20 },
    { "F", 25 }
};

checkedListBox1.Items.AddRange(dictStrToInt.Keys.ToArray());
Now we can get the total of all checked itms;
private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e)
{
    // the Cast here is necessary to enable using Linq
    var chkditems = checkedListBox1.CheckedItems.Cast<string>();

    // using Linq
    int total = chkditems.Sum(cki => dictStrToInt[cki]);

    // for verification only
    Console.WriteLine(total);
}
And, how would this be different from using an Enum ?
checkedListBox1.Items.AddRange(Enum.GetNames(typeof(WkHrs)));

private void checkedListBox1_SelectedValueChanged(object sender, EventArgs e)
{
    var chkditems = checkedListBox1.CheckedItems.Cast<string>();

    int total = chkditems.Sum(cki => (int)Enum.Parse(typeof(WkHrs), cki));

    Console.WriteLine(total);
}
imho, using an Enum is more complex because of the necessary casting, and parsing (in bold, above). While I'm not 100% certain, I bet reflection is involved, and, I believe that is to be avoided.

Questions ?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900