我正在为我的数组使用构造函数,但为什么它说我需要一个返回类型?

时间:2010-04-27 19:58:21

标签: 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 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; }

            IDnumber[] IDnumbers = new IDnumber[3];   

            public GetSchedule() //here i get an error saying method must have a return type???
            {
                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" };    

            }
            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 "";
}
        }
    }
}

2 个答案:

答案 0 :(得分:3)

您已将GetSchedule构造函数放在IDNumber内部类中。将其移到GetSchedule类中。

答案 1 :(得分:0)

GetSchedule构造函数位于错误的类中。它应该是这样的:

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 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; }

            IDnumber[] IDnumbers = new IDnumber[3];   

            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 "";
}
        }

        public GetSchedule() //here i get an error saying method must have a return type???
        {
            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" };

        }

    }
}