CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: array list

  1. #1
    Join Date
    Jan 2010
    Posts
    16

    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

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  3. #3
    Join Date
    Jan 2010
    Posts
    16

    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

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: array list

    Quote Originally Posted by Darenr View Post
    i have to use array list
    Why? Is this homework or something?

  5. #5
    Join Date
    Jan 2010
    Posts
    16

    Re: array list

    it has been solved thank you and yes it was homework. I was missing an object new object call.

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    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.

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: array list

    Quote Originally Posted by HanneSThEGreaT View Post
    Sorry, if this was homework, then your teacher have no clue what he / she is talking about.
    Unfortunately, this is quite common.

  8. #8
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: array list

    Sad, isn't it ?

  9. #9
    Join Date
    Aug 2010
    Posts
    21

    Re: array list

    My instructor says to just Google it.

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: array list

    Quote Originally Posted by JCStorey View Post
    My instructor says to just Google it.
    Maybe you should pay us instead of the instructor?

  11. #11
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Talking 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
  •  





Click Here to Expand Forum to Full Width

Featured