How do I implement in generics

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

    How do I implement in generics

    Hi,
    I am learning generics. I tried to include some elements this way.
    Code:
    List<int> list = new List<int>();
      for (int i=0; i<10; i++)
         {
             list.Add(i);
         }
    This works.
    Now actually i want different types of data to be included
    I want to include empno integer,empname string, dateofjoining date, salary double all these in a generic arraylist.
    How should I do that, i.e. passing different datatypes in a single generic arraylist.
    Kindly help

    Regards
    cmrhema
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Create a new object/class that has all these properties, then create a list of that new object.

    Code:
    public class Employee
    {
         public int empno;
         public int empname;
         // and so on
    }
    
    List<Employee> myEmployees = new List<Employee>();
    Then you reference a given item like

    Code:
    myEmployees[3].empno = 12345;

    Comment

    • cmrhema
      Contributor
      • Jan 2007
      • 375

      #3
      But If I want to iterate in a loop , then how should i do it.
      eg, for (int i=0;i<5;i++)
      {
      //here i should be able to add empno,empname,d oj,salary for 5 records

      }

      Can you pls help me out.

      Comment

      • cmrhema
        Contributor
        • Jan 2007
        • 375

        #4
        I implemented this way

        Code:
        using System;
        using System.Collections.Generic;
        using System.Text;
        
        namespace EmployeeDetails
        {
           public class Emp_Details
            
           {
               
                
                 readonly string _EmpName;
                 readonly int _EmpId;
                 readonly double _EmpSalary;
               
        
               public string EmpName
               {
                   get
                   {
                       return _EmpName;
                   }
               }
               public int EmpId
               {
                   get
                   {
                       return _EmpId;
                   }
               }
               public double EmpSalary
               {
                   get
                   {
                       return _EmpSalary;
                   }
               }
               
            }
            public class GenericClass<T>
            {
        
                List<Emp_Details> myEmpDetails = new List<Emp_Details>();
        
        
            }
            class Program
            {
                static void Main(string[] args)
                {
                    
                }
            }
        }

        Now I want to read n records, I mean user will input the number of records he wants to enter, and then one by one he will enter the employee details,
        Ideas pls

        Comment

        • Bassem
          Contributor
          • Dec 2008
          • 344

          #5
          You defined _EmpName, _EmpId, and _EmpSalary as readonly but I see no constructor to intialize these fields!!!

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Originally posted by cmrhema
            But If I want to iterate in a loop , then how should i do it.
            eg,
            Code:
            for (int i=0;i<5;i++)
            {
            //here i should be able to add empno,empname,doj,salary for 5 records
            
            }
            Can you pls help me out.
            I would suggest you read up on arrays and lists. You need to understand how they work before you can use them. In the code from my previous was code for this.

            Code:
            myEmployees[3].empno = 12345;
            Square brackets with a number [3] is how you access element number 3 of the list myEmployees.

            You need to make a new Employee, set its values, then add it to the list.

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              That's not really a problem though.

              Originally posted by Bassem
              You defined _EmpName, _EmpId, and _EmpSalary as readonly but I see no constructor to intialize these fields!!!
              While it is common to do such things in the constructor, it is by no means required. Since there are public properties that expose the private variables...

              Code:
                     public double EmpSalary
                     {
                         get
                         {
                             return _EmpSalary;
                         }
                     }
              ...there is no reason you couldn't create an empty employee object, then set its values afterward, the add it to the List<>

              Code:
              Emp_Details NewEmployee = new Emp_Details();
              NewEmployee.empno = 5150;
              NewEmployee.EmpSalary = 35000;
              NewEmployee.EmpName = "John Q. Public";
              myEmpDetails.Add(NewEmployee);
              The OP just needs to add a set function to the properties.
              Code:
                     public double EmpSalary
                     {
                         get
                         {
                             return _EmpSalary;
                         }
              [B][I]           set
                         {
                             _EmpSalary = value;
                         }[/I]
              [/B]       }

              Comment

              • Bassem
                Contributor
                • Dec 2008
                • 344

                #8
                Yes, if there is a set in the property. but it'll be used only one time, and that will make ambiguous a little.

                Comment

                • cmrhema
                  Contributor
                  • Jan 2007
                  • 375

                  #9
                  Originally posted by tlhintoq
                  While it is common to do such things in the constructor, it is by no means required. Since there are public properties that expose the private variables...



                  ...there is no reason you couldn't create an empty employee object, then set its values afterward, the add it to the List<>

                  Code:
                  Emp_Details NewEmployee = new Emp_Details();
                  NewEmployee.empno = 5150;
                  NewEmployee.EmpSalary = 35000;
                  NewEmployee.EmpName = "John Q. Public";
                  myEmpDetails.Add(NewEmployee);
                  The OP just needs to add a set function to the properties.
                  Code:
                         public double EmpSalary
                         {
                             get
                             {
                                 return _EmpSalary;
                             }
                  [B][I]           set
                             {
                                 _EmpSalary = value;
                             }[/I]
                  [/B]       }
                  Thank You
                  Solved it after you sent.

                  Comment

                  • tlhintoq
                    Recognized Expert Specialist
                    • Mar 2008
                    • 3532

                    #10
                    Originally posted by Bassem
                    Yes, if there is a set in the property. but it'll be used only one time, and that will make ambiguous a little.
                    We can't really say. Only the OP knows if something will be used only once. I would imagine that some properties will need this type of access. Salary for example changes when people get a raise. Even name changes most times when a woman gets married. If the employee number is in any way coded to reflect a department or division, then it would change with a new job title. So 'set' is needed.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      I think you totally missed Bassem's point. readonly fields can only be initialized on declaration or in the constructor. If the OP didn't initialize them on declaration then they must initialize them in all the constructors that they have. After the initialization, the value cannot be changed.

                      Comment

                      Working...