Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
803 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...