错误:非静态字段,方法或属性需要对象引用

时间:2013-07-07 17:47:06

标签: c# compiler-errors

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;

namespace ClassofEmployees
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
     class employee
    { //will include the attributes of all employees of your organization.

        //fields for employee
      public int employeeId; // 5 digit number to represent employee
      public int ssn; //social security number of employee
      public string name; //employee name
      public int dob; //date of birth
      public int pay; //rate of pay


    }

    class managers : employee
    {
        public string backgroundCheck {get; set;}
        public string isSalary;
        public string responsibilitys;



    }
    private void getEmployeeData(employee employee)
    {

        employee.employeeId = int.Parse(EmployeeID.Text);
        employee.ssn = int.Parse(SSN.Text);
        employee.name = employeeName.Text;
        employee.dob = int.Parse(DOB.Text);
        employee.pay = int.Parse(pay.Text);
        managers.backgroundCheck = bCYes;
        managers.isSalary = salaryYes;
        managers.responsibilitys = responsibilitys.Text;
    }

    private void add_Click(object sender, EventArgs e)
    {
        //create new employee object
        employee newemployee = new employee();
        //get employee data
        getEmployeeData(newemployee);
        //add employee data to new form window list
    }

好的,我完全迷失了我收到的错误。我在我的教科书中都有一个例子。

这是我收到的错误:

错误1非静态字段,方法或属性'ClassofEmployees.Form1.managers.BCY.get'需要对象引用:C:\ Users \ T-Ali \ Desktop \ SHawnasschool \ vb.net 2 c# \ projects \ ClassofEmployees \ ClassofEmployees \ Form1.cs 59 13 ClassofEmployees

我的理解是没有创建对象。但是我相信我用这行代码创建的对象:

 //create new employee object
        employee newemployee = new employee();
        //get employee data
        getEmployeeData(newemployee);
        //add employee data to new form window list

为什么employee.name或任何employee.something工作,但经理部分不会?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

问题是您正在从manager类中读取非静态字段。

managers.backgroundCheck = bCYes;
managers.isSalary = salaryYes;
managers.responsibilitys = responsibilitys.Text;

managers是一个类,而不是对象实例。您需要像创建员工一样创建新的经理对象。