c# - 错误: An object reference is required for the n

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起作用,但是经理部分却不起作用?我怎样才能解决这个问题?

最佳答案

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

managers.backgroundCheck = bCYes;
managers.isSalary = salaryYes;
managers.responsibilitys = responsibilitys.Text;
managers是一个类,而不是对象实例。您需要像对待员工一样创建一个新的经理对象。

关于c# - 错误: An object reference is required for the non-static field, method, or property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17514617/

相关文章:

inheritance - TypeScript 模块引用

android - 如何修复Java lang arrayadapter索引超出范围?

web-services - 我收到错误 “Server Application Unavailab

module - verilog 看不到我的功能

c++ - 获取CUDA错误 “declaration is incompatible with p

class - 在 VBA 中设置类时编译错误

compiler-errors - 访问冲突写入位置…可能是由mpi_get_processor_n

compiler-errors - 端口映射中的语法/逻辑错误

debugging - SASS不一致的缩进错误

c# - 扩展方法找不到类型的定义