generic class doesn't work but ArrayList work fine

T

Tony Johansson

Hello!

This program doesn't work as expected when I use a generic class like this
List<Employee> employeeList = new List<Employee>();.
When the DataGridView is displayed the field from the Worker class is not
displayed.
These two fields are hourlySalary and hourWorked.

But if I replace the generic class and use an ArrayList instead all fields
is displayed both
those in the Employee class and those in the Worker class.
In the code below you can see where I have comment the ArrayList. So if I
just uncomment this
ArrayList and comment out the generic list all works fine.

There is just one row in the database table Employee with the following
contens
PersNr = 19640612-6512
FirstName = Nisse
Lastname = Hult
HireDate = 2005-08-25 00:00:00
HourSalary = 110
HourWorked = 40
EmpType= Worker

I rather want to use the generic class instead of having to use an ArrayList
so can somebody tell me what I have to change to get the generic class to
work.

Here is the code
namespace MyProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void EmployeeToolStripMenuItem_Click(object sender, EventArgs
e)
{
GetEmployees();
}

public void GetEmployees()
{
List<Employee> employeeList = new List<Employee>();
// ArrayList employeeList = new ArrayList();

SqlCeConnection myConnection =
new SqlCeConnection(@"Data
Source=|DataDirectory|\CarManufacturing.sdf");
myConnection.Open();
SqlCeCommand myCommand = new SqlCeCommand("Select * from Employee
", myConnection);
SqlCeDataReader myReader = myCommand.ExecuteReader();

while (myReader.Read())
{
string persNr = myReader["Persnr"].ToString();
string firstName = myReader["FirstName"].ToString();
string lastName = myReader["Lastname"].ToString();
DateTime hireDate = (DateTime)myReader["HireDate"];
string empType = myReader["empType"].ToString().ToUpper();

switch (empType)
{
case "WORKER" :
int hourlySalary = (int)myReader["HourlySalary"];
double hourWorked = (double)myReader["HourWorked"];
Employee worker = new Worker(persNr, firstName,
lastName, hireDate, hourlySalary, hourWorked);
employeeList.Add(worker);
break;
}
}

dataGridView1.DataSource = employeeList;
}
}
}

namespace MyProject
{
public class Employee
{
private string persNr;
private string firstName;
private string lastName;
private DateTime hireDate;

public string PersNr
{
get { return persNr; }
set { persNr = value; }
}

public string FirstName
{
get { return firstName; }
set { firstName = value; }
}

public string LastName
{
get { return lastName; }
set { lastName = value; }
}

public DateTime HireDate
{
get { return hireDate; }
set { hireDate = value; }
}

public Employee(string persNr, string firstName, string lastName,
DateTime hireDate)
{
this.persNr = persNr;
this.firstName = firstName;
this.lastName = lastName;
this.hireDate = hireDate;
}
}

public class Worker : Employee
{
private int hourlySalary;
private double hourWorked;

public Worker(string persNr, string firstName, string lastName,
DateTime hireDate,
int hourlySalary, double hourWorked)
: base(persNr,firstName,lastName,hireDate)
{
this.hourlySalary = hourlySalary;
this.hourWorked = hourWorked;
}

public double HourWorked
{
get { return hourWorked; }
set { hourWorked = value; }
}

public int HourlySalary
{
get { return hourlySalary; }
set { hourlySalary = value; }
}
}
}

//Tony
 
K

kndg

Hello!

This program doesn't work as expected when I use a generic class like this
List<Employee> employeeList = new List<Employee>();.
When the DataGridView is displayed the field from the Worker class is not
displayed.
These two fields are hourlySalary and hourWorked.

...... You bind your DataGridView to List<Employee> but you expected the
DataGridView to show the field for Worker also???
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top