簡體   English   中英

錯誤:非靜態字段,方法或屬性需要對象引用

[英]Error: An object reference is required for the non-static field, method, or property

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是一個類,而不是對象實例。 您需要像對待員工一樣創建一個新的經理對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM