473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I implement in generics

375 256MB
Hi,
I am learning generics. I tried to include some elements this way.
Expand|Select|Wrap|Line Numbers
  1. List<int> list = new List<int>();
  2.   for (int i=0; i<10; i++)
  3.      {
  4.          list.Add(i);
  5.      }  
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
Apr 18 '09 #1
10 1164
tlhintoq
3,525 Expert 2GB
Create a new object/class that has all these properties, then create a list of that new object.

Expand|Select|Wrap|Line Numbers
  1. public class Employee
  2. {
  3.      public int empno;
  4.      public int empname;
  5.      // and so on
  6. }
  7.  
  8. List<Employee> myEmployees = new List<Employee>();
Then you reference a given item like

Expand|Select|Wrap|Line Numbers
  1. myEmployees[3].empno = 12345;
Apr 18 '09 #2
cmrhema
375 256MB
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,doj,salary for 5 records

}

Can you pls help me out.
Apr 18 '09 #3
cmrhema
375 256MB
I implemented this way

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace EmployeeDetails
  6. {
  7.    public class Emp_Details
  8.  
  9.    {
  10.  
  11.  
  12.          readonly string _EmpName;
  13.          readonly int _EmpId;
  14.          readonly double _EmpSalary;
  15.  
  16.  
  17.        public string EmpName
  18.        {
  19.            get
  20.            {
  21.                return _EmpName;
  22.            }
  23.        }
  24.        public int EmpId
  25.        {
  26.            get
  27.            {
  28.                return _EmpId;
  29.            }
  30.        }
  31.        public double EmpSalary
  32.        {
  33.            get
  34.            {
  35.                return _EmpSalary;
  36.            }
  37.        }
  38.  
  39.     }
  40.     public class GenericClass<T>
  41.     {
  42.  
  43.         List<Emp_Details> myEmpDetails = new List<Emp_Details>();
  44.  
  45.  
  46.     }
  47.     class Program
  48.     {
  49.         static void Main(string[] args)
  50.         {
  51.  
  52.         }
  53.     }
  54. }

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
Apr 18 '09 #4
Bassem
344 100+
You defined _EmpName, _EmpId, and _EmpSalary as readonly but I see no constructor to intialize these fields!!!
Apr 18 '09 #5
tlhintoq
3,525 Expert 2GB
@cmrhema
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.

Expand|Select|Wrap|Line Numbers
  1. 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.
Apr 18 '09 #6
tlhintoq
3,525 Expert 2GB
@Bassem
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...

Expand|Select|Wrap|Line Numbers
  1.        public double EmpSalary
  2.        {
  3.            get
  4.            {
  5.                return _EmpSalary;
  6.            }
  7.        }
...there is no reason you couldn't create an empty employee object, then set its values afterward, the add it to the List<>

Expand|Select|Wrap|Line Numbers
  1. Emp_Details NewEmployee = new Emp_Details();
  2. NewEmployee.empno = 5150;
  3. NewEmployee.EmpSalary = 35000;
  4. NewEmployee.EmpName = "John Q. Public";
  5. myEmpDetails.Add(NewEmployee);
  6.  
The OP just needs to add a set function to the properties.
Expand|Select|Wrap|Line Numbers
  1.        public double EmpSalary
  2.        {
  3.            get
  4.            {
  5.                return _EmpSalary;
  6.            }
  7.            set
  8.            {
  9.                _EmpSalary = value;
  10.            }
  11.        }
Apr 18 '09 #7
Bassem
344 100+
Yes, if there is a set in the property. but it'll be used only one time, and that will make ambiguous a little.
Apr 18 '09 #8
cmrhema
375 256MB
@tlhintoq
Thank You
Solved it after you sent.
Apr 19 '09 #9
tlhintoq
3,525 Expert 2GB
@Bassem
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.
Apr 19 '09 #10
r035198x
13,262 8TB
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.
Apr 20 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
13
by: Sherif ElMetainy | last post by:
Hello I was just got VS 2005 preview, and was trying generics. I tried the following code int intArray = new int; IList<int> intList = (IList<int>) intArray; it didn't compile, also the...
2
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
6
by: Venkatesh Bhupathi | last post by:
Hi All, I am trying to inherit the Generic IList<Tinterface to form the typed collection of objects of type T. Following is my sample code, Public Class Record { public string name;...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.