保存对象中的值但无法正确保存 [英] Saving values in an object but won't save properly

查看:82
本文介绍了保存对象中的值但无法正确保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个gui应用程序,我从一个关于公司工资单的文件中读取信息。我将值保存到Employee对象时遇到问题。在调试时我已经看到了我的form1类和Employee类中的值,并且它们在我的表单类中是正确的,但不会改变它们在我的Employee类中初始化的值。



这是我的表格类代码



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);
        }





我还为每个数据成员设置了get and set方法



我尝试了什么:



我试过搜索谷歌的答案,但我很缺乏经验编码,所以我不知道该寻找什么。在调试时我已经看到了我的form1类和Employee类中的值。



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.

推荐答案

有一些问题意味着我们无法根据该代码提供帮助:你没有;显示你在构造函数中调用的方法(并且名称暗示它们应该是属性而不是单独的方法) - 而且很可能它们是问题,因为你显示的代码会影响你的私有变量。



但是......你的openToolStripMenuItem_Click处理程序方法将值存储在一个数组中,该数组在方法结束时被丢弃,所以无论如何它都被浪费了!您的代码的其余部分可以访问的唯一Employee实例是您在表单构造器中创建的单个实例:

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:
private Employee Employee;
...
Employee = new Employee();



总是空白。这可能与你所看到的有关 - 我不知道,你在调试时看不到你的屏幕。



我会建议几个事情:

1)转储EMPLOYEE_MAX值,并停止使用数组来保存您的Employee实例。使用列表< Employee>相反,使用它的Add方法将每个员工添加到集合中。列表< T>就像一个数组,但它扩展为让你拥有你想要的数量。

2)转储Employee变量,并使其成为类级别集合 - List< Employee>从(1) - 并将其初始化为空列表:


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:

private List<Employee> employees = new List<Employee>();

然后,当您想要查看您的员工时,您可以使用该集合的Count属性告诉你有多少,一旦你加载它们就可以使用它们。

3)不要使用Get和Set方法,使你的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:

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;}

并改为使用它们。


这篇关于保存对象中的值但无法正确保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆