Hi,
I am learning generics. I tried to include some elements this way. - 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
10 1164
Create a new object/class that has all these properties, then create a list of that new object. - 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 - myEmployees[3].empno = 12345;
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.
I implemented this way - 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
You defined _EmpName, _EmpId, and _EmpSalary as readonly but I see no constructor to intialize these fields!!!
@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. - 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.
@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... - 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<> -
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. - public double EmpSalary
-
{
-
get
-
{
-
return _EmpSalary;
-
}
- set
-
{
-
_EmpSalary = value;
-
}
- }
Yes, if there is a set in the property. but it'll be used only one time, and that will make ambiguous a little.
@tlhintoq
Thank You
Solved it after you sent.
@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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
by: Mr.Tickle |
last post by:
So whats the deal here regarding Generics in the 2004 release and templates
currently in C++?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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;...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| |