不包含GetDataFromNumber的定义但是我定义了它?

时间:2010-04-27 21:30:57

标签: c# winforms

public void button2_Click(object sender, System.EventArgs e)
{
    string text = textBox1.Text;
    Mainform = this;

    this.Hide();

    GetSchedule myScheduleFinder = new GetSchedule();
    string result = myScheduleFinder.GetDataFromNumber(text);// says there is no definition
    if (!string.IsNullOrEmpty(result))
    {
        MessageBox.Show(result);
    }
    else
    {
        MessageBox.Show("Enter A Valid ID Number!");
    }
}

说它不包含它的定义,但在我的GetSchedule .cs文件中我定义了它

public string GetDataFromNumber(string ID)//defined here
{

    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;
            return myData.ToString();
        }
    }
    return "";
}

GetSchedule 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] =新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(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.ToString());
            }

        public string GetDataFromNumber(string ID)
        {
            IDnumber[] IDnumbers = new IDnumber[3];
            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;
                 return myData.ToString();
    }
}
return "";

}         }

}

}

3 个答案:

答案 0 :(得分:0)

您确定GetDataFromNumber是否在class定义中,而不是在结束括号之后?

答案 1 :(得分:0)

检查GetSchedule类是否与您尝试从中调用它的名称空间相同,或者引用它。

从你的更新帖子中看起来像你的函数GetDataFromNumber在一个名为IDNumber的类中 - 这是问题吗?

尝试:

IDnumber myNumber = new IDnumber();
myNumber.GetDataFromNumber(text);

答案 2 :(得分:0)

问题是因为您正在创建Web表单并将代码从一个页面复制到另一个页面。执行此操作时,必须小心并确保不更改页面中的第一个指令。它告诉Web代码页面后面应该找到什么,它应该找到定义和代码逻辑。您遇到此问题的原因是当您从一个页面复制并粘贴到另一个页面时,它带来了另一个页面的指令,很可能您没有在页面定义中调用的函数。因此,请确保更改了网页中的第一行,以指向其继承的正确.cs文件和类。