سؤال

Hello hi every one below is the program which i am making purpose of the program is that it simply get input and output.....but this error is coming,

Error 1 'method' does not contain a constructor that takes '0' arguments

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

}

هل كانت مفيدة؟

المحلول

Instead of using array of delegate, use multicast delegate. Change the main method as below.

 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();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top