user3174012
user3174012

Reputation: 23

Error: Does not contain a constructor that takes 0 arguments

I am getting the error "Does not contain a constructor that takes 0 arguments" from my c# code below:

 public class Holiday
{

    string startDate, endDate, firstName, lastName, emailAddress, numberOfGuests;
    private double Cost = 100;
    public Holiday(string start, string end, string first, string last, string email, string guestCount)
    {
        startDate = start;
        endDate = end;
        firstName = first;
        lastName = last;
        emailAddress = email;
        numberOfGuests = guestCount;
    }

    public double test
    {
        get { return Cost; }
    }
}
 public class AdventureHoliday : Holiday
{
    public AdventureHoliday(string start, string end, string first, string last, string email, string guestCount)
        : base(start, end, first, last, email, guestCount)
    {
    }
    public double totalcost()
    {
        double adventureAdditional = 0.50;
        double xcost = test + (test * adventureAdditional);
        return xcost;
    }
}

public class CulturalHoliday : Holiday
{

    public CulturalHoliday(string start, string end, string first, string last, string email, string guestCount)
        : base(start, end, first, last, email, guestCount)
    {
    }
    public double totalcost()
    {
        double culturalAdditional = 1.0;
        double xcost = test + (test * culturalAdditional);
        return xcost;
    }

I have looked at the various threads for this problem and they all seem to indicate that the problem should be solved by adding:

: base(start, end, first, last, email, guestCount)

I have added this to the second class and am still getting the error. I am getting this for both the Adventure Holiday and Cultural Holiday on lines 201 and 125 (public class AdventureHoliday : Holiday)

and

public class CulturalHoliday : Holiday

Does anyone have any ideas? Sorry if I have missed something stupid. Thanks in advance

Upvotes: 2

Views: 3690

Answers (2)

Selman Genç
Selman Genç

Reputation: 101681

You need to add a default parameterless constructor manually

public Holiday ()
{ 
   ...
}

Probably you are trying to create a Holiday instance without passing any parameter like this:

var holiday = new Holiday();

or:

var cultural = new CulturalHoliday();

By default all classes inherit a default parameterless constructor from Object class.But if you add a constructor that takes some arguments,you need to add parameterless constructor manually.

Upvotes: 4

ichramm
ichramm

Reputation: 6642

The problem is somewhere else, you shouldn't need a parameterless constructor unless the class is being serialized.

You must provide more information, i.e. the exact line which originates the error, the constructor of the class Holiday (at least the signature), and which classes does class Holiday inherit from.

Upvotes: 0

Related Questions