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

时间:2010-04-27 20:23:43

标签: c# winforms

using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing;
using System.Data.OleDb;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Collections.Generic;

namespace Eagle_Eye_Class_Finder
{

    public class GetSchedule
    {
        public GetSchedule()
        {
            IDnumber[] IDnumbers = new IDnumber[3];
            IDnumbers[0] = new IDnumber() { Name = "Joshua Banks", ID = "900456317", year = "Senior", class1 = "TEET 4090", class2 = "TEET 3020", class3 = "TEET 3090", class4 = "TEET 4290" };
            IDnumbers[1] = new IDnumber() { Name = "Sean Ward", ID = "900456318", year = "Junior", class1 = "ENGNR 4090", class2 = "ENGNR 3020", class3 = "ENGNR 3090", class4 = "ENGNR 4290" };
            IDnumbers[2] = new IDnumber() { Name = "Terrell Johnson", ID = "900456319", year = "Sophomore", class1 = "BUS 4090", class2 = "BUS 3020", class3 = "BUS 3090", class4 = "BUS 4290" };

        }
        public class IDnumber
        {
            public string Name { get; set; }
            public string ID { get; set; }
            public string year { get; set; }
            public string class1 { get; set; }
            public string class2 { get; set; }
            public string class3 { get; set; }
            public string class4 { get; set; }


           public static void ProcessNumber(IDnumber myNum)
                {
                    StringBuilder myData = new StringBuilder();
                    myData.AppendLine(IDnumber.Name);   
                    myData.AppendLine(": ");   
                    myData.AppendLine(IDnumber.ID);   
                    myData.AppendLine(IDnumber.year);   
                    myData.AppendLine(IDnumber.class1);   
                    myData.AppendLine(IDnumber.class2);   
                    myData.AppendLine(IDnumber.class3);   
                    myData.AppendLine(IDnumber.class4);  
                    MessageBox.Show(myData);
                }

            public string GetDataFromNumber(string ID)
            {

               foreach (IDnumber idCandidateMatch in IDnumbers)  

            { 

            if (IDCandidateMatch.ID == ID)
                {
                     StringBuilder myData = new StringBuilder();
                     myData.AppendLine(IDnumber.Name);   
                     myData.AppendLine(": ");   
                     myData.AppendLine(IDnumber.ID);   
                     myData.AppendLine(IDnumber.year);   
                     myData.AppendLine(IDnumber.class1);   
                     myData.AppendLine(IDnumber.class2);   
                     myData.AppendLine(IDnumber.class3);   
                     myData.AppendLine(IDnumber.class4);  
                     return myData;
        }
    }
    return "";
}
        }

    }
}using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing;
using System.Data.OleDb;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Collections.Generic;

namespace Eagle_Eye_Class_Finder
{

    public class GetSchedule
    {
        public GetSchedule()
        {
            IDnumber[] IDnumbers = new IDnumber[3];
            IDnumbers[0] = new IDnumber() { Name = "Joshua Banks", ID = "900456317", year = "Senior", class1 = "TEET 4090", class2 = "TEET 3020", class3 = "TEET 3090", class4 = "TEET 4290" };
            IDnumbers[1] = new IDnumber() { Name = "Sean Ward", ID = "900456318", year = "Junior", class1 = "ENGNR 4090", class2 = "ENGNR 3020", class3 = "ENGNR 3090", class4 = "ENGNR 4290" };
            IDnumbers[2] = new IDnumber() { Name = "Terrell Johnson", ID = "900456319", year = "Sophomore", class1 = "BUS 4090", class2 = "BUS 3020", class3 = "BUS 3090", class4 = "BUS 4290" };

        }
        public class IDnumber
        {
            public string Name { get; set; }
            public string ID { get; set; }
            public string year { get; set; }
            public string class1 { get; set; }
            public string class2 { get; set; }
            public string class3 { get; set; }
            public string class4 { get; set; }


           public static void ProcessNumber(IDnumber myNum)
                {
                    StringBuilder myData = new StringBuilder();
                    myData.AppendLine(IDnumber.Name);   
                    myData.AppendLine(": ");   
                    myData.AppendLine(IDnumber.ID);   
                    myData.AppendLine(IDnumber.year);   
                    myData.AppendLine(IDnumber.class1);// i get it for all of these   
                    myData.AppendLine(IDnumber.class2);   
                    myData.AppendLine(IDnumber.class3);   
                    myData.AppendLine(IDnumber.class4);  
                    MessageBox.Show(myData);
                }

            public string GetDataFromNumber(string ID)
            {

               foreach (IDnumber idCandidateMatch in IDnumbers)  

            { 

            if (IDCandidateMatch.ID == ID)
                {
                     StringBuilder myData = new StringBuilder();
                     myData.AppendLine(IDnumber.Name);   
                     myData.AppendLine(": ");   
                     myData.AppendLine(IDnumber.ID);   
                     myData.AppendLine(IDnumber.year);   
                     myData.AppendLine(IDnumber.class1);   
                     myData.AppendLine(IDnumber.class2);   
                     myData.AppendLine(IDnumber.class3);   
                     myData.AppendLine(IDnumber.class4);  
                     return myData;
        }
    }
    return "";
}
        }

    }
}

4 个答案:

答案 0 :(得分:0)

静态方法不应该引用自身,它应该引用你传递给它的IDnumber,如下所示:

public static void ProcessNumber(IDnumber myNum)
{
    StringBuilder myData = new StringBuilder();
    myData.AppendLine(myNum.Name);
    myData.AppendLine(": ");
    myData.AppendLine(myNum.ID);
    myData.AppendLine(myNum.year);
    myData.AppendLine(myNum.class1);// i get it for all of these   
    myData.AppendLine(myNum.class2);
    myData.AppendLine(myNum.class3);
    myData.AppendLine(myNum.class4);
    MessageBox.Show(myData.ToString());
}

答案 1 :(得分:0)

好了很多代码,但这是一个例子

public class IDnumber 
    { 
        public string Name { get; set; } 
        public string ID { get; set; } 
        public string year { get; set; } 
        public string class1 { get; set; } 
        public string class2 { get; set; } 
        public string class3 { get; set; } 
        public string class4 { get; set; } 


       public static void ProcessNumber(IDnumber myNum) 
            { 
                StringBuilder myData = new StringBuilder(); 
                myData.AppendLine(IDnumber.Name);    
                myData.AppendLine(": ");    
                myData.AppendLine(IDnumber.ID);    
                myData.AppendLine(IDnumber.year);    
                myData.AppendLine(IDnumber.class1);// i get it for all of these    
                myData.AppendLine(IDnumber.class2);    
                myData.AppendLine(IDnumber.class3);    
                myData.AppendLine(IDnumber.class4);   
                MessageBox.Show(myData); 
            } 

你的方法是静态的,但是包含你所引用的成员的类不是,它不会那样工作,因为该方法永远不属于某个实例。

答案 2 :(得分:0)

 public static void ProcessNumber(IDnumber myNum)
 {
                    StringBuilder myData = new StringBuilder();
                    myData.AppendLine(myNum.Name);   
                    myData.AppendLine(": ");   
                    myData.AppendLine(myNum.ID);   
                    myData.AppendLine(myNum.year);   
                    myData.AppendLine(myNum.class1);   
                    myData.AppendLine(myNum.class2);   
                    myData.AppendLine(myNum.class3);   
                    myData.AppendLine(myNum.class4);  
                    MessageBox.Show(myData);
 }

答案 3 :(得分:0)

一种解决方案是:

        foreach (IDnumber idCandidateMatch in IDnumbers)  

        { 

        if (idCandidateMatch.ID == ID)
            {
                 StringBuilder myData = new StringBuilder();
                 myData.AppendLine(idCandidateMatch.Name);   
                 myData.AppendLine(": ");   
                 myData.AppendLine(idCandidateMatch.ID);   
                 myData.AppendLine(idCandidateMatch.year);   
                 myData.AppendLine(idCandidateMatch.class1);   
                 myData.AppendLine(idCandidateMatch.class2);   
                 myData.AppendLine(idCandidateMatch.class3);   
                 myData.AppendLine(idCandidateMatch.class4);  
                 return myData;
    }

而不是:

       foreach (IDnumber idCandidateMatch in IDnumbers)  

        { 

        if (IDCandidateMatch.ID == ID)
            {
                 StringBuilder myData = new StringBuilder();
                 myData.AppendLine(IDnumber.Name);   
                 myData.AppendLine(": ");   
                 myData.AppendLine(IDnumber.ID);   
                 myData.AppendLine(IDnumber.year);   
                 myData.AppendLine(IDnumber.class1);   
                 myData.AppendLine(IDnumber.class2);   
                 myData.AppendLine(IDnumber.class3);   
                 myData.AppendLine(IDnumber.class4);  
                 return myData;
    }
}
return "";

因为Name(和其他属性)不是静态的。

问题出在以下几行:

的ID号。[属性]

例如

IDnumber.ID

IDnumber.Name

因为ID,Name ......和其他不是静态的。解决方案是将类名替换为所有相关行中的实例变量:

idCandidateMatch.ID

idCandidateMatch.Name

... (见上面的代码)

请注意,您在此处使用不同的变量名称大小写:

        foreach (IDnumber *idCandidateMatch* in IDnumbers)  

在这里:

        if (*IDCandidateMatch*.ID == ID)

C#是区分大小写的,不应该工作(除非我在源代码中遗漏了一些东西:)。