Specifically accessing a generic arraylist

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmrhema
    Contributor
    • Jan 2007
    • 375

    Specifically accessing a generic arraylist

    Hi,
    I have an generic array list
    I have created a class employee details and declared variables such as empid,empname, empdepartement, empsalary etc.
    I have inserted the values as a object
    i.e. i have declared
    Code:
    public class GenericClass<T>
        {
             List<Emp_Details> myEmpDetails= new List<Emp_Details>();
            public void adddetails(Emp_Details id)
            {
                
                myEmpDetails.Add(id);
            }
    }
    Now I have added the values in the main program as
    Code:
    GenericClass<Emp_Details> gc1 = new GenericClass<Emp_Details>();
     Emp_Details id;
    gc1.adddetails(id);
    i have added details in the below manner
    1. Enter total no of depts.
    2. for i = 1 to depts
    (Added, department name, employee name, etc...)

    Now I want the output as below
    Department Name=Chemical
    list of employess
    Department Name=Computer
    List of employess

    Code:
    If i use for (i=0;i<myempdetails.count;i++)
    {//list all employees}
    I get the department name in each and every count,
    But I want to be displayed Department Wise.

    How do I do that?
    Suggestions pls
    Regards
    cmrhema
    Last edited by tlhintoq; Apr 20 '09, 04:34 AM. Reason: Please use [CODE]...your code here...[/CODE] tags
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    1. Plan. 2. Code

    The right way to do this would be to use a multiple table database. One table has the details of an employee. One table has a list of departments and the employee numbers of the staff within that. And so on.

    But lets say you don't want to go that far... Let's see what we can come up with for a plan B.

    Perhaps starting with a List<string> that is just the departments. Use that list in a combobox as part of data entry screen for the employee. This way the employee can only be assigned to a valid department and not some to "Human Resources", some to "H.R.", some to "HR" and so on. This gives you a list of departments you can use to break down your report.

    Then you are going to use a set of nested loops (one inside the other)

    Your outer loop will go through all the entries of the List<string> Departments.
    Your inner loop will go through all the employees and compare the employee department to the current outer loop department. If they match, put it in the report at that time.

    Code:
    foreach (string DepartmentName in Departments)
    {
       foreach(employee EmployeeDetail in Employees)
       {
          if (DepartmentName == EmployeeDetail.Department)
          {
             // Do some cool reporting
          }
       }
    }

    Comment

    • cmrhema
      Contributor
      • Jan 2007
      • 375

      #3
      Thanks, i will consider it and revert back, sorry for the code tags. Secondly its a pure console application.

      Comment

      • cmrhema
        Contributor
        • Jan 2007
        • 375

        #4
        Thanks tlhintoq I got it.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Not sure what you would need to revert, but all of the code snippets will work.
          Since you can't use a combobox, you might want to make a list of departments, and offer the user a choice 1=HR, 2=Accounting, 3=Mail room and so on. Store the number in a List<int> and you are good.

          Comment

          Working...