I know exactly what to do. I'm going to give you two solutions: one is more basic, one more advanced. You may want to keep it more simple, which will allow you to avoid downloading my code, etc.
Actually your example is the perfect one to demonstrate right technique (my 5!).
Now, your 5 skills are combined, as your have check boxes, not radio boxes, right? It means the skill set is represented by just 5 bits. Let's follow simple logic. Best way to implement bit set data is this:
[System.Flags]
enum SkillSet : byte {
None = 0,
A = 1 << 1,
B = 1 << 2,
C = 1 << 3,
D = 1 << 4,
E = 1 << 5,
}
In other words,
A = 1, B = 2, C = 4,..
etc.
To check up a skill in a skill set, use:
static bool HasSkill(SkillSet skillSet, SkillSet skill) {
return (skillSet & skill) == skill;
}
And this is how to combine skills:
SkillSet mySkills = SkillSet.A | SkillSet.C | SkillSet.E;
This is how to populate your controls with some value of
SkillSet Value
:
chkA.Checked = HasSkill(Value, SkillSet.A);
chkB.Checked = HasSkill(Valie, SkillSet.B);
This is how to extract SkillSet Value from controls when editing is done by the UI user:
Value = SkillSet.None;
if (chkA.Checked == true)
SkillSet |= SkillSet.A;
if (chlA.Checked == true)
SkillSet |= SkillSet.B;
That's it.
You don't need any list. Complete skill set of one person is expressed with a single bit set of the size of byte.
Also, note you don't have to use any hard-coded string values. The only place use any constants is the definition of the enumeration type itself -- perfectly supportable way.
Even the name of the controls can be generated out of this enumeration type. Pay attention for the attribute
System.Flags
.
This attribute only effect string representation of the value of the enumeration type.
Consider
SkillSet value = (SkillSet)5
, same as
SkillSet value = SkillSet.A + SkillSet.C
. With the attribute, value.ToString() will return "5"; using
Flags
attribute will return "A, C" -- try it.
A little note on the check up of the boolean value
chkA.Checked == true
. Frankly, just
chkA.checked
would do the same, but only if the property has the type
bool
.
The property of the WPF check box uses different type:
bool?
. This type is
nullable, that is, instead of usual boolean value set of
{False, True}
it uses 3-value set:
{null, False, True}
. So, the check would not return
bool
, it would return
bool?
.
Learn .NET, Ladies and Gentlemen! It's a bit deeper then you might thought.
As a next step, I will advice somewhat more advanced technique, base of my relatively recent work,
this one.
Thank you very much for attention!