2013-07-07 69 views
0
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:\用戶\ t -Ali \ Desktop \ SHawnasschool \ vb.net 2 c#\ projects \ ClassofEmployees \ ClassofEmployees \ Form1.cs 59 13職員類別

我的理解是該對象沒有被創建。不過我相信我對象跟這行代碼創建:

//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工作,但經理部分不會?我怎樣才能解決這個問題?

+0

錯誤表明你的'managers'類有一個名爲'BCY'的屬性,但是這不會出現在你的問題中。你確定這是導致錯誤的確切代碼嗎? – Lee

+0

我很抱歉,我已經更改了代碼,希望它可以解決問題,但它沒有,並且我忘記將其更改回來,我編輯了我的帖子並修復了代碼,使其完全像我的錯誤一樣。 –

回答

3

問題是您正在閱讀manager類中的非靜態字段。

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

managers是一個類,而不是一個對象實例。您需要像創建員工一樣創建新的管理員對象。

+0

'getEmployeeData'是'Form1'的實例成員,而不是'employee'。 – Lee

+0

@lee - 是的,你說得對。試圖通讀它略有混淆。看起來問題是以靜態方式使用'managers'類。 – keyboardP

+0

鍵盤非常感謝你!我創建了一個新的管理對象,現在一切都在流動! –

相關問題