Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
812 views
in Technique[技术] by (71.8m points)

c# - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...