-2

I am confused to why this will not run.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Buyer : User
    {
        public void AuctionWon()
        {
        }
    }
}

I am getting "does not contain a constructor that takes 0 arguments". I have searched for help before hand but no results where helpful.

This is the user class

public class User
{

    private int accountNo;
    private int password;

    public User(int accountNo, int password)
    {
        this.accountNo = accountNo;
        this.password = password;

    }

    public bool Validatepassword(int userpassword)
    {
        if (password == userpassword)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public int GetAccountNo()
    {
        return accountNo;
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Davison89
  • 45
  • 2

4 Answers4

12

It seems like your base class (User class), does not have any constructor that has 0 arguments.

Your user class has a constructor something like:

public class User
{
    public User(int accountNo, int password)
    {
        this.accountNo = accountNo;
        this.password = password;

    }
}

and Buyer must be Inherited like:

    public class Buyer : User
    {
        public Buyer(int accountNo, int password) : base(accountNo, password)
        {
        }

        public void AuctionWon()
        {
        }
    }
Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
2

This error occurs if the base class (User) has a constructor that takes at least one argument, but does not have a constructor with no arguments.

So this is OK:

class User
{
}

class Buyer : User
{
    public void AuctionWon()
    {
    }
}

Because user does not have any constructors, a default "hidden" constructor with 0 arguments is automatically created for you by the compiler.

But this will cause the error you saw ("'User' does not contain a constructor that takes '0' arguments"):

class User
{
    public User(string text)
    {
    }
}

class Buyer : User
{
    public void AuctionWon()
    {
    }
}

Because User now has an explicit constructor, no default constructor of 0 arguments will be created by the compiler. You could add a 0 argument constructor to User, but often in this situation the designer of the User class intends you to call the User constructor and supply an appropriate argument, e.g.,

class User
{
    public User(string text)
    {
    }
}

class Buyer : User
{
    public Buyer() : base("Adam")
    {
    }

    public void AuctionWon() 
    {
    }
}

We now have added a constructor to Buyer that calls the existing User constructor, supplying an appropriate argument to it.

Polyfun
  • 9,479
  • 4
  • 31
  • 39
0

When you run your program, this is the error that you get

'ConsoleApplication3.User' does not contain a constructor that takes 0 arguments

This message is self explanatory that there is no parameterless constuctor in User class.

Add a parameterless constructor in User class (if it editable and not in some dll. otherwise not possible) something like

public User()
{}

OR

if user contains constructor with parameter, use that like this

public class Buyer : User
{
    public Buyer() : base(FooBar)
    {
    }
}
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
  • That's assuming a parameterless constructor makes sense for that class. It may not. The OP may also not have the ability to modify that class. – Servy Mar 18 '13 at 17:25
  • 2
    Your syntax for calling the base class's constructor is incorrect. – Servy Mar 18 '13 at 17:28
0

I suspect that your User class does not contain a constructor with no arguments. So when constructing an instance of Buyer it isn't know how to construct the base class.

In order to solve it: create an empty constructor in your Buyer class and have it call a constructor from the base class (User).

René Wolferink
  • 3,558
  • 2
  • 29
  • 43