There is no argument given that corresponds to the required formal parameter - .NET Error

后端 未结 5 1077
迷失自我
迷失自我 2020-11-29 11:26

I have been re-factoring one of my old MSSQL Connection helper library and I got the following error:

Severity Code Description Project File Li

相关标签:
5条回答
  • 2020-11-29 11:48

    I received this same error in the following Linq statement regarding DailyReport. The problem was that DailyReport had no default constructor. Apparently, it instantiates the object before populating the properties.

    var sums = reports
        .GroupBy(r => r.CountryRegion)
        .Select(cr => new DailyReport
        {
            CountryRegion = cr.Key,
            ProvinceState = "All",
            RecordDate = cr.First().RecordDate,
            Confirmed = cr.Sum(c => c.Confirmed),
            Recovered = cr.Sum(c => c.Recovered),
            Deaths = cr.Sum(c => c.Deaths)
        });
    
    0 讨论(0)
  • 2020-11-29 11:50

    In the constructor of

     public class ErrorEventArg : EventArgs
    

    You have to add "base" as follows:

        public ErrorEventArg(string errorMsg, string lastQuery) : base (string errorMsg, string lastQuery)
        {
            ErrorMsg = errorMsg;
            LastQuery = lastQuery;
        }
    

    That solved it for me

    0 讨论(0)
  • 2020-11-29 11:51

    I got the same error but it was due to me not creating a default constructor. If you haven't already tried that, create the default constructor like this:

    public TestClass() {

    }

    0 讨论(0)
  • 2020-11-29 11:56

    You have a constructor which takes 2 parameters. You should write something like:

    new ErrorEventArg(errorMsv, lastQuery)
    

    It's less code and easier to read.

    EDIT

    Or, in order for your way to work, you can try writing a default constructor for ErrorEventArg which would have no parameters, like this:

    public ErrorEventArg() {}
    
    0 讨论(0)
  • 2020-11-29 11:56

    I got this error when one of my properties that was required for the constructor was not public. Make sure all the parameters in the constructor go to properties that are public if this is the case:

    using statements namespace someNamespace

    public class ExampleClass {
    
      //Properties - one is not visible to the class calling the constructor
      public string Property1 { get; set; }
      string Property2 { get; set; }
    
       //Constructor
       public ExampleClass(string property1, string property2)
      {
         this.Property1 = property1;
         this.Property2 = property2;  //this caused that error for me
      }
    }
    
    0 讨论(0)
提交回复
热议问题