【问题标题】:Does not contain a definition for GetDataFromNumber But i have it defined?不包含 GetDataFromNumber 的定义但我已经定义了吗?
【发布时间】:2011-02-13 02:16:22
【问题描述】:
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 类

公共类 GetSchedule { 公共GetSchedule() { 身份证号码[]身份证号码=新身份证号码[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(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 "";

} }

}

}

【问题讨论】:

  • 对不起,你需要重新表述你的问题,它一点也不清楚。
  • 对不起,我收到一条错误消息,指出未定义 GetDataFromNumber 并且不接受类型的第一个参数..但是 GetDataFromNumber 在另一个 .cs 文件的第二组代码中使用/定义.. .我能做些什么来纠正这个!?!
  • 你真的有一个叫做GetSchedule的类吗?如果是这样,在问题中包含此定义可能会有所帮助。
  • 是的,我确实有一个名为 GetSchedule 的类

标签: c# winforms


【解决方案1】:

检查 GetSchedule 类是否在您尝试调用它的同一个命名空间中,或者它是否被引用。

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

试试:

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

【讨论】:

  • 我所有的 .cs 文件都在同一个项目中,并且在同一个命名空间 Eagle_Eye_Class_Finder....
  • 好的 - 我已经更新了我的答案。从您发布的代码中很难分辨,但您的函数似乎不在您要实例化的类中
  • 是的,你是对的,它在 IDnumber 类中,但它是公共的,所以我不应该能够在我的整个项目中使用它吗?
  • 是的——但你必须实例化正确的类。如果您实例化 IDnumber 类,那么您将能够使用它来调用它。
  • 好吧,这是有道理的,我对 C# 有点陌生,任何帮助我如何以正确的方式实例化它?
【解决方案2】:

您是否确定GetDataFromNumberclass 定义内,而不是在右大括号之后?

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2011-02-13
      • 1970-01-01
      • 2018-07-08
      • 1970-01-01
      • 2022-08-13
      • 1970-01-01
      • 2021-12-04
      • 2013-06-16
      • 1970-01-01
      相关资源
      最近更新 更多