Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I have two classes. Admin.cs and Arog.cs
 
Prog.cs inherits Admin.cs
 
admin.cs consists of 3 variables (surname,id,salary)
prog.cs consists of the 3 variables in Admin.cs and 5 different skills
 
the 5 skills consist of 5 check boxes where I can check(ie Tick) any of them maybe even more than 1
 
I want to be able to store these 5 skills in a generic list (the reason of not using an array is because prog can have either 1 skill or even up to 5 skills)
 
Once I have the necessary check boxes checked and name,id,salary y pressing a command button add, I want to further add the object in a list box.
 
this is what I am using for the admin and it works OK
 
c = new admin(txtSurname.Text, txtID.Text, Convert.ToDouble(txtSalary.Text));

 
when i am adding the to the prog I just get
smith 123YRR1 18000.00 System.collections.GenericList'1[System.String]
 
List<string> sk = new List<string>();
if (chkA.Checked == true)
          {
              skills.Add("A");
          }
          if (chkB.Checked == true)
          {
              skills.Add("B");
          }
          if (chkC.Checked == true)
          {
              skills.Add("C");
          }
          if (chkD.Checked == true)
          {
              skills.Add("D");
          }
          if (chkE.Checked == true)
          {
              skills.Add("E");
          }
 
c = new Prog(txtSurname.Text, txtID.Text,Convert.ToDouble(txtSalary.Text),sk);
 
//class prog
class Prog : Admin
    {
        //string skills;
        List<string> sk; //= new List<string>();

        public prog(string surname, string id, double salary,List<string>sk)
            : base(surname,id,salary)
        {
            this.sk = sk;
        }
        public override string ToString()
        {
            return base.ToString()+ this.sk;
        }
 
How can I pass the generic list ? so that I can fill my list box ?
 
example the list box should show
 
smith 123YRR1 18000.00 A C D
 
where A C D are skills
Posted 10-Dec-10 9:56am
bmw318mt1.1K
Edited 11-Dec-10 21:55pm
v5
Comments
William Winner at 10-Dec-10 18:52pm
   
Just an FYI, but classes don't have extensions. You have two files called Admin.cs and Arog.cs but you could have any number of classes inside of those files.
Abdul Quader Mamun at 12-Dec-10 3:55am
   
Spelling Check.
Mark Nischalke at 10-Dec-10 16:22pm
   
I don't really see a question here.
bmw318mt at 10-Dec-10 16:26pm
   
I have updated the question
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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,
} //enum SkillSet

 
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;
//..., etc.
 
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!
  Permalink  
v5
Comments
SAKryukov at 12-Dec-10 19:01pm
   
Answer is updated because some earlier answers I was arguing against have been removed.
Espen Harlinn at 26-Feb-11 10:53am
   
Good reply - a 5
SAKryukov at 26-Feb-11 20:15pm
   
Thank you.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

I promised to add more advanced recipe then that of Answer 1.
 
To use my method, you need to see my article Enumeration Types do not Enumerate! Working around .NET and Language Limitations (Generic classes for enumeration-based iteration and array indexing) and download the code.
 
We need to use this:
 
using SA.Universal.Enumerations;
 
My library offers generic classes for iteration through enumeration values and array indexed by enumeration members. Your application requires them both. For base enumeration type we can use the one shown in Answer 3, with one modification:
 
[System.Flags]
enum SkillSet {
    [NonEnumerable] None = 0,
    A = 1 << 1,
    B = 1 << 2,
    C = 1 << 3,
    D = 1 << 4,
    E = 1 << 5,
} //enum SkillSet
 
We shall need two variables in you form, one for iteration, another to store an array of check boxes:
 
Enumeration<SkillSet> SkillSetEnumeration
    = new Enumeration<SkillSet>();
EnumerationIndexedArray<SkillSet, CheckBox> SkillSetControl
    = new EnumerationIndexedArray<SkillSet, CheckBox>();
 
Check boxes can be created during run-time and automatically set up according to the enumeration type.
 
We need to have definition of for some check boxes layout somewhere:
 
const int CheckBoxTop = 8;
const int CheckBoxInterval = 2;
const int CheckBoxLeft = 4;
 
A good place to call the following set up method is at the end of form constructor (or any time later, but before showing the form).
 
void Populate() {
    Control parent = this.panelTests;
    int current = CheckBoxTop;
    foreach (var item in SkillSetEnumeration) {
        CheckBox cb = new CheckBox();
        parent.Controls.Add(cb);
        cb.Top = current;
        cb.Left = CheckBoxLeft;
        current += cb.Height + CheckBoxInterval;
        cb.Text = item.Name;
        SkillSetControl[item.EnumValue] = cb;
   } //loop
} //void Populate
 
And, finally, now we can have a UI property which gets SkillSet value from a set of check box control or translates (sets) this value to the Checked state of those controls:
 
SkillSet SkillSetControlValue {
     get { //controls to value
         SkillSet value = SkillSet.None;
         foreach (var item in SkillSetEnumeration) {
             if (SkillSetControl[item.EnumValue].Checked)
                 value |= item.EnumValue;
         } //loop
         return value;
     } //SkillSetControlValue get
     set { //values to control
         foreach (var item in SkillSetEnumeration)
             SkillSetControl[item.EnumValue].Checked =
                 (item.EnumValue & value) > 0;
         } //SkillSetControlValue set
} //SkillSetControlValue
 
That's all!
  Permalink  
v2
Comments
Espen Harlinn at 26-Feb-11 10:54am
   
Amazing effort, my 5
SAKryukov at 26-Feb-11 20:15pm
   
Thank you very much.
Here I trying to use my article on enumeration, first in the cycle of 3.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I think this might be what you're looking for:
 
class Prog : Admin
    {
    List<string> sk = new List<string>();
 
    // ...

 
    public override string ToString()
        {
        return base.ToString() + " " + string.Join(" ", sk);
        }
    }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this (no guarantees):
 
listbox.Items.AddRange(myGenericList.ToArray());
 
If this is marked as the answer, why is it voted a 1?
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The ListBox is using the ToString() method of the Generic List which returns a description of it (which is what you see) NOT it's contents.
 
To get that you will have to write your own class which inherits from List<> and in that class write your own ToString() method which returns a string in the format you like.
 
Just had a thought. It might be better to use a BitArray[^], which is ideal for storing an 'array' of true/false values. Of course you would still need a ToString() method for that.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 349
1 Nirav Prabtani 268
2 _Amy 185
3 CHill60 170
4 Richard Deeming 135
0 OriginalGriff 8,004
1 Sergey Alexandrovich Kryukov 7,017
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 13 Dec 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100