简体   繁体   中英

Creating a new object in C#

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);
        }
    }
}

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);

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);

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, I suspect you want the following constructor:

public Car()
{
//some stuff
}

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM