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