0

您好,下面的每个人都是我正在制作的程序,该程序的目的是它只是获取输入和输出.....但是这个错误来了,

错误 1“方法”不包含采用“0”参数的构造函数

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    public delegate void method();
    namespace Question5
    {
       public class student
       {
         private int Reg;
         private int course;
         private float gpa;
         private string Batch;
         private string Name;

        public void Setpersonalinfo()
        {
              Console.WriteLine("Enter The Name:");
              Name =Console.ReadLine();
              Console.WriteLine("Enter The Reg Num:");
              Reg = int.Parse(Console.ReadLine());
              Console.WriteLine("Enter The Batch:");
              Batch = Console.ReadLine();
        }
        public void getpersonalinfo()
        {
              Console.WriteLine("Entered Name :  {0}",Name);
              Console.WriteLine("Entered Reg Num:  {0}",Reg);
              Console.WriteLine("Entered Batch:   {0}",Batch);
        }
        public void setcourseinfo()
        {
              Console.WriteLine("How many courses you have Registered?");
              course = int.Parse(Console.ReadLine());
        }
        public void getcourseinfo()
        {
              Console.WriteLine("Your registered courses are:   {0}",course);
        }
        public void setgpa()
        {
              Console.WriteLine("Pl Enter you GPA ");
              gpa = float.Parse(Console.ReadLine());
        }
        public void getgpa()
        {
              Console.WriteLine("Your GPA is:  {0}",gpa);
        }
        static void Main(string[] args)
        {
             //int num;
              student s1 = new student();
             //Console.WriteLine("For how Many Students you want to enter record?");
             //num = int.Parse(Console.ReadLine());
             method[] d1 = new method[2];
             for (int i = 0; i < d1.Length; i++)
             d1[i] = new method();
             for(int i=0;i<2;i++)
             {

                  Console.WriteLine("Enter The Personal Details:");
                  d1[i] = new method(s1.Setpersonalinfo);

                  Console.WriteLine("Enter The Courses:");
                  d1[i] = new method(s1.setcourseinfo);

                  Console.WriteLine("Enter The GPA:");
                  d1[i] = new method(s1.setgpa);

             }
             for (int i = 0; i <2; i++)
             {
                  Console.WriteLine("Your Entered all details are:");
                  d1[i] = new method(s1.getpersonalinfo);
                  d1[i] = new method(s1.getcourseinfo);
                  d1[i] = new method(s1.getgpa);
             }
             Console.ReadLine();
           }
        }

}

4

1 回答 1

0

而不是使用array of delegate,使用multicast delegate。改变main方法如下。

 student s1 = new student();
 method d;                        
 d = new method(s1.Setpersonalinfo);            
 d += new method(s1.setcourseinfo);
 d += new method(s1.setgpa);            
 d += new method(s1.getpersonalinfo);
 d += new method(s1.getcourseinfo);
 d += new method(s1.getgpa);

 for (int i = 0; i < 2;i++ )
      d();  

 Console.ReadKey();
于 2013-11-12T17:40:19.857 回答