Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is for a gui application where I read info from a file about a companies payroll. I am having trouble saving the values into my Employee object. While debugging I have watched the values inside my form1 class and Employee class and they are correct in my form class but will not change from the values they are initialized in in my Employee class.

Here is my form class code

public partial class Form1 : Form
    {
        //declare constants
        const int EMPLOYEE_MAX = 10;

        //a class level reference to Employee
        private Employee Employee;

        public Form1()
        {
            InitializeComponent();
            
            //create an Employee object
            Employee = new Employee();
        }

        //The aboutToolStripMenuItem Method
        //Purpose: To inform the user about the program
        //Parameters: The object generating the event
        //and the events arguments
        //Returns: None
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Garrett Bills\nCS 1400\nProject 12");
        }

        //The exitToolStripMenuItem Method
        //Purpose: To allow the user to exit the program
        //Parameters: The object generating the event
        //and the events arguments
        //Returns: None
        private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //The openToolStripMenuItem
        //Purpose: To allow the user to select a file to open
        //Paramets: The object generating the event
        //and the events arguments
        //Returns: None
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //declare variables and arrays
            int employeeNumber1 = 0;
            string employeeName1 = "";
            string employeeAddress1 = "";
            double hourlyWage1 = 0.0;
            double hoursWorked1 = 0.0;
            string fileInput = "";
            int numEmployees = 0;
            Employee[] employeeInfo = new Employee[EMPLOYEE_MAX];            
            string[] hourInfoInput = new string[EMPLOYEE_MAX];

            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "text files (*.txt)|*txt";

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    StreamReader employeeData = new StreamReader(myStream);
                    
                    //begin if statement
                    while (fileInput != null)
                    {
                        fileInput = employeeData.ReadLine();

                        if (fileInput == null)
                        {
                            break;
                        }
                        else
                        {
                            
                            employeeNumber1 = int.Parse(fileInput);
                            fileInput = employeeData.ReadLine();
                            employeeName1 = fileInput;
                            Employee.SetEmployeeName(employeeName1);
                            fileInput = employeeData.ReadLine();
                            employeeAddress1 = fileInput;
                            fileInput = employeeData.ReadLine();
                            hourInfoInput = fileInput.Split();
                            hourlyWage1 = double.Parse(hourInfoInput[0]);
                            hoursWorked1 = double.Parse(hourInfoInput[1]);
                            employeeInfo[numEmployees++] = new Employee(employeeNumber1, employeeName1, employeeAddress1,
                                 hourlyWage1, hoursWorked1);
                            numEmployees++;
                        }
                        
                    }                    
                }
            }
        }


and here is my code from my employee class
//declare data members
        private int employeeNumber;
        private string employeeName;
        private string employeeAddress;
        private double hourlyWage;
        private double hoursWorked;

        //default constructor
        //Purpose: Initialize all values to zeros or empty strings
        //Parameters: None
        //Returns: None
        public Employee()
        {
            employeeNumber = 0;
            employeeName = "";
            employeeAddress = "";
            hourlyWage = 0.0;
            hoursWorked = 0.0;
        }

        //parameterized constructor
        //Purpose: to initialize all values
        //Parameters: An int(the employee number), two strings (the employees name and
        //address), and two doubles(the employees hourly wage and  hours worked)
        //Returns: None
        public Employee(int empNum, string empName, string empAdd, 
            double hrWage, double hrsWrk)
        {
            employeeNumber = empNum;
            SetEmployeeNumber(empNum);
            employeeName = empName;
            SetEmployeeName(empName);
            employeeAddress = empAdd;
            SetEmployeeAddress(empAdd);
            hourlyWage = hrWage;
            SetHourlyWage(hrWage);
            hoursWorked = hrsWrk;
            SetHoursWorked(hrsWrk);
        }


I also have a get and set method for every data member

What I have tried:

I have tried searching google for answers but I am very inexperienced when it comes to coding so I am not sure what to look for. While debugging I have watched the values inside my form1 class and Employee class.
Posted
Updated 28-Apr-17 21:49pm

1 solution

There are a few problems which mean we can't help based on just that code: you don;t show the methods that you call within the constructor (and the names imply they should be properties rather than separate methods) - and it's very likely that they are the problem since the code you show affects your private variables.

But...your openToolStripMenuItem_Click handler method stores the values in an array which is discarded at the end of the method so it's all wasted anyway! The only Employee instance the rest of your code can access is the single instance you create in your form construtor:
C#
private Employee Employee;
...
Employee = new Employee();

Which is always blank. This may be related to what you are seeing - I don;t know, I can't see your screen while you are debugging.

I would suggest a couple of things:
1) Dump the EMPLOYEE_MAX values, and stop using an array to hold your Employee instances. Use a List<Employee> instead and use it's Add method to add each employee to the collection. A List<T> is like an array, but it expands to let you have as many as you want in there.
2) Dump the Employee variable, and make that a class level collection - the List<Employee> from (1) - and initialise it to an empty list:
C#
private List<Employee> employees = new List<Employee>();
Then when you want to look at your employees, you can use the Count property of the collection to tell you how many there are, and they are always available to the class once you have loaded them.
3) Don't use Get and Set methods, make your Employee elements actual properties:
C#
public int EmployeeNumber {get; set;}
public string EmployeeName {get; set;}
public string EmployeeAddress {get; set;}
public double HourlyWage {get; set;}
public double HoursWorked {get; set;}
And use them instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900