0

I have generated this simplified version of my problem:

public class Variable
{
    public Variable(string s, int i)
    {

    }
    public Variable(string str) : base(str, 0) // error here
    {

    }
}

Clearly I have a constructor that take 2 arguments. But the error is saying that I don't.

I am confused.

I am using .NET Standard 2.0

Please ask for any additional clarification.

Muzib
  • 2,412
  • 3
  • 21
  • 32

2 Answers2

3

base class (object in your case) doesn't have such a constructor

 object(string s, int i)

But your current class this does have the required constructor:

public class Variable
{
    public Variable(string s, int i)
    {

    }

    public Variable(string str) : this(str, 0) // current class constructor call
    {

    }
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • thanks for saving my day.. I knew only about `base()` to overload contructors in c#.. Is it (use of `this`) only in .net-standard or everywhere in c# ? – Muzib Oct 02 '18 at 20:51
  • @Muzib: `this` as a current class constructor can be used *everywhere* in c# – Dmitry Bychenko Oct 02 '18 at 20:52
3
: base(str, 0)

is calling the Object constructor which doesn't have one for 2 parameters.

use this instead

: this(str, 0)
Matti Price
  • 3,351
  • 15
  • 28