Sample Scenario
- Your GUI has 5 checkboxes for various settings.
- [ ] Created
- [ ] Updated
- [ ] Deleted
- [ ] Renamed
- These could all be on, or any combination thereof.
- You need to store it in an int column in your database
- More settings may be added in the future
[Flags] private enum EventsToWatchFor: int{
Created = 1,
Updated = 2,
Deleted = 4,
Renamed = 8,
All = Created Updated Renamed Deleted
}
Step 2: Use the & Operator to EvaluateAssume you get a value from the database and you need to de-construct it and place checkmarks in the appropriate checkboxes. I won't go into how you get the data value, but we will assume it is in a variable called val with a value of 5. A value of 5 is essentially a "Create" orred with a "Delete".
if (((EventsToWatchFor)val & EventsToWatchFor.Created) == EventsToWatchFor.Created)
{
chkCreated.Checked = true;
}
You could tighten up the code and do it like this:
chkCreated.Checked = ((EventsToWatchFor)val & EventsToWatchFor.Created) == EventsToWatchFor.Created;
0 comments:
Post a Comment