-
August 20th, 2010, 06:44 PM
#1
array list
I have an array list of objects such as Employee Object
Each object contains string firstName, string lastName, double rate.
my question is how can i iterate through the array list and have the members of each object appear in text boxes such as first name will be in a text box, last name will be in a different text box, rate will be in its own text box as well.
any help would be greatly appreciated.
i tried something like this just to see if the names were in there and all i get is the array name
-
August 20th, 2010, 09:11 PM
#2
Re: array list
First off, there is no good reason to use an array list for this. Use a typed generic list.
Code:
List<Employee> list = new List<Employee>();
to iterate through a list, just use a foreach.
-
August 21st, 2010, 08:21 PM
#3
Re: array list
i have to use array list
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace EmployeePayrollClasses
{
public partial class GUI : Form
{
//private Employee[] employee = new Employee[3];
private double gross;
private int _i = 0;
public ArrayList empList = new ArrayList();
List<Employee> list = new List<Employee>();
Employee emp = new Employee( );
StreamReader employeeStreamReader;
public GUI()
{
InitializeComponent();
}
private void GUI_Load(object sender, EventArgs e)
{
openFile();
}
private void calcPay()//calculate method
{
double hours;//variables
double payrate;
double ot;
double otPay;
string th;
string tr;
th = txtHours.Text;
tr = txtRate.Text;
try//try catch for errors
{
txtHours.BackColor = (Color.White);
hours = Double.Parse(th);//get input
payrate = double.Parse(tr);
if (hours <= 40)//test for 40 and below hours
{
gross = hours * payrate;
txtGross.Text = gross.ToString("c2");
}
else
if (hours > 40)//test for overtime
{
ot = hours - 40;
otPay = ot * (payrate * 1.5);
gross = (hours * payrate) + otPay;
txtGross.Text = gross.ToString("c2");
}
}
catch (FormatException ex)
{
MessageBox.Show(" ENTER HOURS PLEASE");
txtHours.BackColor = (Color.Pink);
txtHours.Text = "";
}
}
private void displayRecord()//display the records
{
try
{
emp = /*(Employee)empList[_i]; */(empList[_i] as Employee);
txtFName.Text = emp.firstName;
txtLName.Text = emp.lastName;
txtRate.Text = emp.rate.ToString();
_i++;
}
catch (Exception ex)
{
txtFName.Text = string.Empty;
txtLName.Text = string.Empty;
txtRate.Text = string.Empty;
_i = 0;
MessageBox.Show("Pretty error message" + ex.ToString());
}
}
//try
//{
// foreach (Employee e in list)
// {
// Console.WriteLine(e.firstName.ToString());
// Console.WriteLine(e.lastName.ToString());
// }
// txtFName.Text = emp.firstName;
// txtLName.Text = emp.lastName;
// txtRate.Text = emp.rate.ToString();
// // _i++;
//}
//{
//if (_i < list.Count && _i == 0)//cycle through the elements
//{
// //txtFName.Text = emp.firstName;
// //txtLName.Text = emp.lastName;
// //txtRate.Text = emp.rate.ToString();
// txtFName.Text = list[_i].firstName;
// txtLName.Text = list[_i].lastName;
// txtRate.Text = list[_i].rate.ToString();
// _i++;
// }
//}
//else
//{
// _i = 0;//go back to element 0
// txtFName.Text = "";
// txtLName.Text = "";
// txtRate.Text = "";
//}
// }
//catch (Exception ex)
//{
// MessageBox.Show("hmm " + ex.ToString());
//}
//}
public void openFile()//open the files
{
DialogResult responseDialogResult;
openFileDialog1.InitialDirectory = Directory.GetCurrentDirectory();
responseDialogResult = openFileDialog1.ShowDialog();
int x = 0;
try//place info into the structure
{
employeeStreamReader = File.OpenText(openFileDialog1.FileName);
while (employeeStreamReader.Peek() > -1)
{
//employee[x].firstName = employeeStreamReader.ReadLine();
//employee[x].lastName = employeeStreamReader.ReadLine();
//employee[x].rate = Convert.ToDouble(employeeStreamReader.ReadLine());
//x++;
emp.first = employeeStreamReader.ReadLine();
emp.last = employeeStreamReader.ReadLine();
emp.rate = Convert.ToDouble(employeeStreamReader.ReadLine());
empList.Add(emp);
//list.Add(emp);
x++;
}
}
catch (FormatException fe)
{
MessageBox.Show("ERROR " + fe.ToString());
}
}
private void btnNext_Click(object sender, EventArgs e)
{
if (_i == empList.Count) _i = 0;
displayRecord();//call this method
txtHours.BackColor = (Color.White);//change color back
txtHours.Text = "";//clear fields
txtGross.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
employeeStreamReader.Close();
}
private void btnCalc_Click(object sender, EventArgs e)
{
calcPay();
// txtGross.Text = gross.ToString();
}
}
}
I still get the same error -- it only shows me the last element in the file.
for instance file contains
tom
follow
10
tim
hast
20
john
sprat
12.5
it is only showing john sprat 12.5
3 times instead of each element
-
August 22nd, 2010, 08:21 PM
#4
Re: array list
Originally Posted by Darenr
i have to use array list
Why? Is this homework or something?
-
August 23rd, 2010, 08:19 PM
#5
Re: array list
it has been solved thank you and yes it was homework. I was missing an object new object call.
-
August 24th, 2010, 03:33 AM
#6
Re: array list
Sorry, if this was homework, then your teacher have no clue what he / she is talking about. Use the right tools for the job - your teacher should know that by now.
Oh, and please mark your thread resolved when your questions have been answered :
http://www.codeguru.com/forum/showthread.php?t=401115
Last edited by HanneSThEGreaT; August 24th, 2010 at 03:36 AM.
-
August 24th, 2010, 11:55 AM
#7
Re: array list
Originally Posted by HanneSThEGreaT
Sorry, if this was homework, then your teacher have no clue what he / she is talking about.
Unfortunately, this is quite common.
-
August 24th, 2010, 02:20 PM
#8
Re: array list
Sad, isn't it ?
-
August 25th, 2010, 11:22 AM
#9
Re: array list
My instructor says to just Google it.
-
August 25th, 2010, 05:55 PM
#10
Re: array list
Originally Posted by JCStorey
My instructor says to just Google it.
Maybe you should pay us instead of the instructor?
-
August 26th, 2010, 01:03 AM
#11
Re: array list
JCStory, that is a common term in the teaching circles nowadays, sadly. To roughly translate teacher language :
"Google it!"
to normal layman's terms it will mean :
"I don't know, so if you find it out, let me know" LOL!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|