So I have created a car class with objects and constructor and I'm trying to get the values but I'm getting an error which says: There is no argument given that corresponds to the required formal parameter c# This is my class:
public class Car
{
private int yearOfProduction;
private int engineVolume;
public Car(int yearOfProduction, int engineVolume)
{
this.yearOfProduction = yearOfProduction;
this.engineVolume = engineVolume;
}
public int YearOfProduction { get; set; }
public int EngineVolume { get; set; }
}
}
And my main method:
using System;
namespace Car2
{
class Program
{
static void Main()
{
Car car = new Car();
Console.Write("Year of production: ");
car.YearOfProduction = int.Parse(Console.ReadLine());
Console.Write("Engine volume: ");
car.EngineVolume = int.Parse(Console.ReadLine());
Console.WriteLine(YearOfProduction);
Console.WriteLine(EngineVolume);
}
}
}
CodePudding user response:
You've got a few different issues here.
Starting with the Car
class:
You only have one constructor, no default constructor to hit with new Car()
. Adjust your class to something like this:
public class Car
{
private int yearOfProduction;
private int engineVolume;
public Car(int yearOfProduction, int engineVolume)
{
this.yearOfProduction = yearOfProduction;
this.engineVolume = engineVolume;
}
public Car() //this is what will be called on new Car()
{
}
public int YearOfProduction { get; set; }
public int EngineVolume { get; set; }
}
You can otherwise leave the class as is by calling the constructor you provided, which requires two int
s, like Car car = new Car(5, 5)
.
In your Main
, the lines
Console.WriteLine(YearOfProduction);
Console.WriteLine(EngineVolume);
Are not referring to the properties of car
. You need to adjust that to:
Console.WriteLine(car.YearOfProduction);
Console.WriteLine(car.EngineVolume);
CodePudding user response:
You specify that the class needs two parameters in order to be created: yearOfProduction
and engineVolume
, both without default value, so both mandatory.
public Car(int yearOfProduction, int engineVolume)
{
this.yearOfProduction = yearOfProduction;
this.engineVolume = engineVolume;
}
but you initialize the instance without the parameters:
Car car = new Car();
For the sanity of your code, remove the initialization, or set default values:
public Car(int yearOfProduction = 0, int engineVolume = 0)
{
this.yearOfProduction = yearOfProduction;
this.engineVolume = engineVolume;
}
Or initialize with values:
Car car = new Car(0,0);
CodePudding user response:
You only constructor for Car
as you have written it is:
public Car(int yearOfProduction, int engineVolume)
This means you can only Create an instance of Car
if you give it yearOfProduction
and engineVolume
. Something like this:
int year = 1994;
int volume = 2400;
Car car = new Car(year,volume);
Alternatively you could provide a second constructor:
public Car()
{
YearOfProduction = 1994;
EngineVolume = 2400;
}
By the way, when you say "a car class with objects" I think what you mean is "a car class with some fields and properties". Also, your fields aren't doing anything and seem redundant.
CodePudding user response:
The problem is that your constructor is expecting two values
public Car(int yearOfProduction, int engineVolume)
And you instantiate the class with none:
Car car = new Car();
Given the way you instantiate the class, you should add (or replace, depending on your use cases) the following constructor:
public Car()
{
//some stuff
}