Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am new to .Net. We have a task to build to classes Department, Employee. In Department, two properties DeptId,DeptName. In Employee, four properties EmpId,EmpName,EmpSalary , object of Department. So that department class values will get automatically bounded to employee class. We need to populate field values with help of collections.
We tried like this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace employee
{
    public class Department
    {
        public int deptId { get; set; }
        public string deptName { get; set; }
    }
    public class Employee
    {
        private employee.Department dept;

        public Employee(employee.Department dept)
        {
            // TODO: Complete member initialization
            this.dept = dept;
        }
        public int empId { get; set; }
        public string empName { get; set; }
        public int emPSalary { get; set; }
        public Department Department { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList list = new ArrayList();
            Department dept = new Department();
            dept.deptId = 1;
            dept.deptName = "IT";
            list.Add(dept);
            dept = new Department();
            dept.deptId = 2;
            dept.deptName = "HR";
            list.Add(dept);
            ArrayList list1 = new ArrayList();
            Employee emp = new Employee(dept);
            emp.empId = 100;
            emp.empName = "anusha";
            emp.emPSalary = 10000;
            emp.Department = dept;
            list1.Add(emp);
            emp = new Employee(dept);
            emp.empId = 101;
            emp.empName = "usha";
            emp.emPSalary = 20000;
            emp.Department = dept;
            list1.Add(emp);

            var query = from Employee employee in list1
                        where employee.empId == 100
                        select employee;
            foreach(Employee e in query)
            {
                Console.WriteLine(e.empId+""+e.empName +" "+e.emPSalary+""+e.Department+"");

                Console.ReadLine();
            }
            
        }
    }
}


While debuging with F10key we are getting only second values of department class i.e DeptId=2 at Employee emp = new Employee(dept);.

Any Input..
Thanks
Sindhu
Posted

1 solution

C#
Department dept = new Department();
dept.deptId = 1;
dept.deptName = "IT";
list.Add(dept);
dept = new Department();
dept.deptId = 2;
dept.deptName = "HR";
list.Add(dept);

After these lines dept has values 2 and 'HR'!
And later you use dept and not the what you stored in the array!
C#
emp.Department = dept;

So bot employee works at the same department!
 
Share this answer
 
Comments
sindhukrothapalli 7-Jan-14 2:45am    
Hi Kornfeld Eliyahu Peter, For this what we have to do. Any Suggestion.. to get complete details of department.
Kornfeld Eliyahu Peter 7-Jan-14 2:48am    
Try reorder you code.
1. create department 1
2. use department 1 on employee 1
3. create department 2
4. use department 2 on employee 2
sindhukrothapalli 7-Jan-14 2:57am    
Thanks sir. Its Working.
Kornfeld Eliyahu Peter 7-Jan-14 3:10am    
I know! :-)
Karthik_Mahalingam 7-Jan-14 3:14am    
hi sindhu,
if your issue is resolved, pls mark this as answer and close this post...

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