Why am I getting the error "does not contain a constructor that takes 0 arguments"? FROM#

On my form upload, I have this code:

private void Form1_Load(object sender, EventArgs e) { CharityCyclists cyclist1 = new CharityCyclists(); CharityCyclists cyclist2 = new CharityCyclists("a", 1, "Finished", 0, 0, 0, "One Wheel", 1, 500); cyclist1.Type = "Novelty Charity Cyclist"; cyclist1.Number = 1; cyclist1.Finished = "Not Finished"; cyclist1.Hours = 0; cyclist1.Mins = 0; cyclist1.Secs = 0; cyclist1.Bicycle = "Tricycle"; cyclist1.Wheels = 3; cyclist1.FundsRaised = 300; } 

However, I get an error: "CycleEvent.CharityCyclists" does not contain a constructor that takes 0 arguments ", it says that the error is related to this part of the code:

 CharityCyclists cyclist1 = new CharityCyclists(); 

Here is my CharityCyclists class:

 class CharityCyclists : Cyclists { private string bicycle; private int wheels; private double fundsRaised; public string Bicycle { get { return bicycle; } set { bicycle = value; } } public int Wheels { get { return wheels; } set { wheels = value; } } public double FundsRaised { get { return fundsRaised; } set { fundsRaised = value; } } public CharityCyclists(String type, int number, String finished, int hours, int mins, int secs, string bicycle, int wheels, double fundsRaised) : base(type, number, finished, hours, mins, secs, fundsRaised) { this.bicycle = bicycle; this.wheels = wheels; this.FundsRaised = fundsRaised; } public override string ToString() { return base.ToString() + " riding a " + bicycle + " with " + wheels + " wheels" ; } } 

Thanks!

+7
source share
10 answers

This is because the CharityCyclists class CharityCyclists not have a constructor that takes no arguments.

The C # compiler will create a default constructor for you if you do not define other constructors. If you define the constructor yourself (like you do), the C # compiler will not generate a default constructor.

If you want to allow the construction of CharityCyclists without parameters, add this code to the class:

 public CharityCyclists() {} 
+17
source

When you provide a constructor for your class that takes arguments, the compiler no longer creates an empty constructor.

Therefore, you cannot call an empty constructor because it does not exist. You will need to explicitly write a constructor that takes 0 arguments in your class code.

+2
source

If you declare constructor classes, you will not automatically get the default constructor. You will need to create a constructor without parameters to solve the problem or call one that takes parameters.

+1
source

You do not have a constructor that takes no arguments.

you need to add

 public CharityCyclists() { this.bicycle = "bike"; this.wheels = 2; this.FundsRaised = 0; } 

or something like that

+1
source

When you create a constructor that does not contain 0 arguments, you automatically delete the default constructor. You must create a new default constructor (no arguments), and this will take care of this problem.

+1
source

If you don't have constructors, C # will implicitly create an empty one for you. This is functionally the same as you write:

 public CharityCyclists() { } 

It only does this if you do not have constructors. You have one, so this does not happen. You need to explicitly create a constructor that takes no parameters.

+1
source

You do not have a constructor for this class that has no arguments. The only constructor you have has options. Add this to your class:

 public CharityCyclists() { } 
+1
source

Add this to the CharityCyclists class:

 public CharityCyclists() { } 

You cannot encode a string:

 CharityCyclists cyclist1 = new CharityCyclists(); 

if you do not have this constructor.

0
source

In .NET, a constructor with no arguments (no parameters) implicitly exists if no other constructors are declared. After declaring a constructor with parameters, you must also explicitly declare another constructor without parameters to continue compiling the code.

0
source

You are trying to create an instance of cyclist1 of an instance of the CharityCyclists class without any arguments - a constructor with no arguments is required.

But the definition of the CharityCyclists class contains only one constructor with 9 arguments. Since 9 arguments are required for this constructor, cyclist1 will not match this constructor. You need a constructor that takes no arguments, as in:

 public CharityCyclists() { this.bicycle =""; this.wheels = 0; this.FundsRaised = 0.0; } 
0
source

All Articles