0

I am currently in the process of trying to create a car class that performs the following: providing the year, make, and speed. I was able to get majority of the code completed but currently getting the following error when trying to create my car object. Also I try to put in as much comments as I can to understand what exactly is my code doing. Please correct my comments if they are wrong, so I can better understand what exactly those lines of code is doing. Below is my code:

public partial class Form1 : Form
{
    // Declaring the class
    private Car myCar;

    public Form1()
    {
        InitializeComponent();
    }

    // GetCarData method that accepts Car object as an argument
    private void GetCarData()
    {
        // Creating the car object
        Car myCar = new Car();

        try
        {
            // Get the car year and model
            myCar.year = int.Parse(yearTextBox.Text);

            myCar.make = makeTextBox.Text;

            myCar.speed = 0;

        }
        catch (Exception ex)
        {
            // Display error message
            MessageBox.Show(ex.Message);
        }
    }

    // Display speed data when either acceleration or break button is clicked
    private void accelerateButton_Click(object sender, EventArgs e)
    {
        GetCarData();
        myCar.accelerate(5);
        MessageBox.Show("Your car is traveling at " + myCar.speed + " mph.");
    }

    private void breakButton_Click(object sender, EventArgs e)
    {
        GetCarData();
        myCar.brake(5);
        MessageBox.Show("Your car is traveling at " + myCar.speed + " mph.");
    }
}

I am getting "There is no argument given that corresponds to the required formal parameter 'year' of 'Car.Car(int,string,int)'" error

Here is my class:

class Car
{
    // Fields 
    private int _year; // Year of the car
    private string _make; // Make of the car
    private int _speed; // Speed of the car

    //Constructor
    public Car(int year, string make, int speed)
    {
        _year = 2010;
        _make = "Mitsubishi";
        _speed = 0;
    }

    // Year property
    public int year
    {
        get { return _year; }
        set { _year = value; }
    }

    // Make property
    public string make
    {
        get { return _make; }
        set { _make = value; }
    }

    // Speed property
    public int speed
    {
        get { return _speed; }
        set { _speed = value; }
    }

    public void accelerate(int increaseSpeed)
    {
        // When speed button is pushed, it increases the speed
        speed = speed + increaseSpeed;
    }

    public void brake(int decreaseSpeed)
    {
        // When break button is pushed, it decreases the speed
        speed = speed - decreaseSpeed;
    }


}
Quang P.
  • 77
  • 9
  • 5
    What tutorial are you following? It seems to be pretty old. You have one immediate problem: `Car myCar = new Car();` is not valid because Car does not have a parameterless constructor, you need to pass it year, make and speed. You have another problem, as soon as you fix the previous one, which is that you never assign the `myCar` field, but instead create a `myCar` variable that is discarded when the method exits – Camilo Terevinto Oct 27 '18 at 19:07
  • 1
    You probably want `Car myCar = new Car(int.Parse(yearTextBox.Text), makeTextBox.Text, 0)`. Alternativly add a parameterless constructor to your `Car`-class. – MakePeaceGreatAgain Oct 27 '18 at 19:08
  • @HimBromBeere Take out `Car` from `Car myCar` and that's what I meant – Camilo Terevinto Oct 27 '18 at 19:09
  • @HimBromBeere thanks for the advice, now I am currently getting another error within the same line: System.FormatException: 'Input string was not in a correct format.' – Quang P. Oct 27 '18 at 19:16

1 Answers1

-1

In the GetCarData() method, instead of making an empty Car object and then setting its attributes, you should use your constructor:

Car myCar = new Car(int.Parse(yearTextBox.Text), int.Parse(yearTextBox.Text), 0);

You are getting this error because the compiler is looking for a default constructor in your Car class which is not present. Refer to Should we always include a default constructor in the class? for more information.

Kushagra Goyal
  • 252
  • 1
  • 8