Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
EDIT: I am getting a new error after having added the () to the Car instantiation.
It says:
'Car_Class_BBrantley.Form1.myCar()' must declare a body because it is not marked abstract, extern, or partial
Alright, so my task is to create an application that displays 3 main features: year, make, and speed of a car. The year and make are inputted with textboxes and the speed starts at 0.
There is an accelerate button which is supposed to add 5 to the speed every time it is pressed and a brake button which decreases the speed by 5 every time it is pressed.
I am having trouble using the class and form together to display the results. I need to display in a messagebox the make, year, and speed. I have been sitting here for hours and I am getting nowhere. I am getting the errors
speed does not exist in current context
and
car does not exist in current context
under my buttons. I am unsure of how I should go about fixing this.
Any and all help is much appreciated. I'm sorry if this is a mess. I have never worked with classes before.
Here is the form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Car_Class_BBrantley
{
public partial class Form1 : Form
{
private Car myCar;
public Form1()
{
myCar = new Car;
InitializeComponent();
}
private void GetCarData(Car car)
{
try {
myCar.Make = txtMake.Text;
myCar.Year = int.Parse(txtModel.Text);
myCar.Speed = 0;
}
catch (Exception ex)
{
MessageBox.Show(string.Concat("Must enter a valid make and year model for the car. ", ex.Message, "\r\n", ex.StackTrace));
}
}
private void btnAcc_Click(object sender, EventArgs e)
{
GetCarData();
myCar.AccSpeed(5);
MessageBox.Show(" Your car is a " + myCar.Year + myCar.Make + " and it is traveling " + myCar.Speed + " mph. ");
}
private void btnBrake_Click(object sender, EventArgs e)
{
GetCarData();
myCar.DecSpeed(5);
MessageBox.Show(" Your car is a " + myCar.Year + myCar.Make + " and it is traveling " + myCar.Speed + " mph. ");
}
}
}
If you would like to see the class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Car_Class_BBrantley
{
class Car
{
private int year;
private string make;
private int speed;
public Car()
{
this.year = 1994;
this.make = "Ford";
this.speed = 0;
}
public Car(string make, int year, int speed)
{
this.year = year;
this.make = make;
this.speed = speed;
}
public string Make
{
get { return make; }
set { make = value; }
}
public int Year
{
get { return Year; }
set { Year = value; }
}
public int Speed
{
get { return speed; }
set { speed = value; }
}
public void AccSpeed(int speedIncrement)
{
//Add check for speed limit ranges
Speed += speedIncrement;
}
public void DecSpeed(int speedDecrement)
{
//Add check for speed limit ranges
Speed -= speedDecrement;
}
}
}
You're instantiating your Car class incorrectly:
myCar = new Car; // wrong
myCar = new Car();
You're not passing your Car instance to the GetCarData() method even though it expects it:
GetCarData(); // wrong
GetCarData(myCar);
(You're not actually using the instance of Car that you're passing to the method... either remove the parameter from the method, or reference it in your code.)
1-You are not using parentheses in the Car constructor:
myCar = new Car;
should be
myCar = new Car();
2-You declared GetCarData to expect a parameter but it's not using it and when you call it you are not passing it.
Change
private void GetCarData(Car car)
To
private void GetCarData()
Use parentheses within your class name to create an instance of the class:
myCar = new Car();
And change your method definition to:
private void GetCarData()
The method expecting a parameter of type Car, but you are calling it without providing any parameter.As far as I can see you are not using the parameter in the method so you can just remove it.Otherwise you have to pass a Car instance to your method.
Related
I have a program that is suppose to collect user car data in one method/class and then use it later.
In this case, user types in Year: 2001 Make: Mustang Model: GT (click Make the Car Button). The textboxes are cleared. Then click accelerate. In the DetailLabel it should increment speed by 5 every time the button is pressed. Output: The speed of 2001 Mustang GT is 5 mph. Same for the Brake Button. The speed of 2001 Mustang GT is 0 mph.
Problem: I can't access/use my collected data for brake or accelerate buttons.
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CarMaker_Tate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void GetCarData()
{
CarClass myCar = new CarClass();
int CYear;
string CMake = MakeTextBox.Text;
String CModel = ModelTextBox.Text;
if (int.TryParse(YearTextBox.Text, out CYear) && CYear >= 1900 && CYear <= 2022)
{
if (CMake != "")
{
if (CModel != "")
{
myCar.Year = CYear;
myCar.Make = CMake;
myCar.Model = CModel;
}
else
MessageBox.Show("Please enter a car model");
}
else
MessageBox.Show("Please enter a car make");
}
else
MessageBox.Show("Please enter a year between 1900 - 2022");
}
private void MakeButton_Click(object sender, EventArgs e)
{
GetCarData();
YearTextBox.Text = "";
MakeTextBox.Text = "";
ModelTextBox.Text = "";
}
private void AccelerateButton_Click(object sender, EventArgs e)
{
myCar.AccSpeed(5);
DetailLabel.Text = "The speed of " + myCar.Year + myCar.Make + myCar.Model + "is" + myCar.Speed + "mph";
}
private void Summarybutton_Click(object sender, EventArgs e)
{
Summary mySummaryForm = new Summary();
mySummaryForm.Show();
}
private void BrakeButton_Click(object sender, EventArgs e)
{
myCar.DecSpeed(5);
DetailLabel.Text = "The speed of " + myCar.Year + myCar.Make + myCar.Model + "is" + myCar.Speed + "mph";
}
}
}
CarClass.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarMaker_Tate
{
class CarClass
{
int CarYear;
string CarMake;
string CarModel;
public static int CarCount;
public CarClass()
{
// constructor runs everytime a car is created
CarYear = 0;
CarMake = "";
CarModel = "";
CarCount++;
}
public int Year
{
get
{
return CarYear;
}
set
{
CarYear = value;
}
}
public string Make
{
get { return CarMake; }
set { CarMake = value; }
}
public string Model
{
get { return CarModel; }
set { CarModel = value; }
}
public int Speed
{
get { return Speed; }
set { Speed = value; }
}
public void AccSpeed(int speedIncrement)
{
//Add check for speed limit ranges
Speed += speedIncrement;
}
public void DecSpeed(int speedDecrement)
{
//Add check for speed limit ranges
Speed -= speedDecrement;
}
}
}
You should declare myCar as a variable outside the GetCarData() method. Otherwise only the method GetCarData can access it. You can even think to make the variable static:
Like that:
public partial class Form1 : Form
{
// declare myCar for the entire class
private static CarClass myCar;
public Form1()
{
InitializeComponent();
}
private void GetCarData()
{
// init myCar
myCar = new CarClass();
...
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm having, what I hope, is a small issue with my code. The "Current Speed" is supposed to increase by 5 every time I hit the "Accelerate" button and go down by 5 when I hit the "Brake" button. Currently, when I hit "Accelerate it shows I'm going 5, but will not increase past that. When I hit "Brake" it just shows -5. I feel like I've been staring at this for a few hours now and I feel like it's probably something simple I'm missing. Any help would be greatly appreciated.
Here is my form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CarClass
{
public partial class Form1 : Form
{
private Car myCar;
public Form1()
{
myCar = new Car();
InitializeComponent();
}
private void GetCarData()
{
try
{
myCar.Make = carMakeText.Text;
myCar.Year = int.Parse(carYearText.Text);
myCar.Speed = 0;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void accButton_Click(object sender, EventArgs e)
{
GetCarData();
myCar.AccSpeed(5);
enteredMakeLabel.Text = myCar.Make;
enteredYearLabel.Text = myCar.Year.ToString();
currentSpeedLabel.Text = myCar.Speed.ToString();
}
private void brakeButton_Click(object sender, EventArgs e)
{
GetCarData();
myCar.DecSpeed(5);
enteredMakeLabel.Text = myCar.Make;
enteredYearLabel.Text = myCar.Year.ToString();
currentSpeedLabel.Text = myCar.Speed.ToString();
}
}
}
Here is my class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarClass
{
class Car
{
private int year;
private string make;
private int speed;
public Car()
{
this.year = 1994;
this.make = "Ford";
this.speed = 0;
}
public Car(string make, int year, int speed)
{
this.year = year;
this.make = make;
this.speed = speed;
}
public string Make
{
get { return make; }
set { make = value; }
}
public int Year
{
get { return year; }
set { year = value; }
}
public int Speed
{
get { return speed; }
set { speed = value; }
}
public void AccSpeed(int speedIncrement)
{
speed += speedIncrement;
}
public void DecSpeed(int speedDecrement)
{
speed -= speedDecrement;
}
}
}
That's because in GetCarData(); you set the speed back to 0. If you remove that line the code will do what you expect.
EDIT: I have tried this a different way and updated my new code. I am now getting a few errors:
1. 'Car_Class_BBrantley.Car.Car()' must declare a body because it is not marked abstract, extern, or partial
2. No overload for method 'GetCarData' takes 0 arguments
3. No overload for method 'GetCarData' takes 0 arguments
These last two errors fall under the GetCarData(); lines which are under the two button sections.
Alright, so my task is to create an application that displays 3 main features: year, make, and speed of a car. The year and make are inputted with textboxes and the speed starts at 0.
There is an accelerate button which is supposed to add 5 to the speed every time it is pressed and a brake button which decreases the speed by 5 every time it is pressed.
I am having trouble using the class and form together to display the results. I need to display in a messagebox the make, year, and speed. I have been sitting here for hours and I am getting nowhere. I am getting the errors " speed does not exist in current context" and "car does not exist in current context" under my buttons. I am unsure of how I should go about fixing this.
Any and all help is much appreciated. I'm sorry if this is a mess. I have never worked with classes before.
Here is the form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Car_Class_BBrantley
{
public partial class Form1 : Form
{
private Car myCar;
public Form1()
{
myCar = new Car;
InitializeComponent();
}
private void GetCarData(Car car)
{
try {
myCar.Make = txtMake.Text;
myCar.Year = int.Parse(txtModel.Text);
myCar.Speed = 0;
}
catch (Exception ex)
{
MessageBox.Show(string.Concat("Must enter a valid make and year model for the car. ", ex.Message, "\r\n", ex.StackTrace));
}
}
private void btnAcc_Click(object sender, EventArgs e)
{
GetCarData();
myCar.AccSpeed(5);
MessageBox.Show(" Your car is a " + myCar.Year + myCar.Make + " and it is traveling " + myCar.Speed + " mph. ");
}
private void btnBrake_Click(object sender, EventArgs e)
{
GetCarData();
myCar.DecSpeed(5);
MessageBox.Show(" Your car is a " + myCar.Year + myCar.Make + " and it is traveling " + myCar.Speed + " mph. ");
}
}
}
///////////////////////////////////////////////////////////////////////////////////////
If you would like to see the class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Car_Class_BBrantley
{
class Car
{
private int year;
private string make;
private int speed;
public Car()
{
this.year = 1994;
this.make = "Ford";
this.speed = 0;
}
public Car(string make, int year, int speed)
{
this.year = year;
this.make = make;
this.speed = speed;
}
public string Make
{
get { return make; }
set { make = value; }
}
public int Year
{
get { return Year; }
set { Year = value; }
}
public int Speed
{
get { return speed; }
set { speed = value; }
}
public void AccSpeed(int speedIncrement)
{
//Add check for speed limit ranges
Speed += speedIncrement;
}
public void DecSpeed(int speedDecrement)
{
//Add check for speed limit ranges
Speed -= speedDecrement;
}
}
}
This code:
public int AccSpeed
{
get { return Speed + 5; }
}
.. says "get a copy of the value in property Speed and increase that copy with five before returning the result"
What you want is: "increase the value of property Speed with five and then return the result". That is done by using:
public int AccSpeed
{
get
{
Speed = Speed + 5; //shorter code: Speed += 5;
return Speed;
}
}
However, Properties should never change state of classes in the get methods. Anyone using your code would get really confused if it did.
Instead use a method to make it crystal clear:
public int Accelerate()
{
Speed += 5;
return Speed;
}
You can declare your variables like this:
public int name { get; set; }
this automatically makes it a variable with a getter and setter.
As for the methods that increase speed etc. you could make a method like this fx:
public void AccSpeed() {
Speed += 5;
}
As for the rest you should be able to figure it out from this.
EDIT: Elaboration.
As I think your confused on how classes work let me elaborate. In your class name you have included the type of car I'm guessing because the class is supposed to represent that certain type of car.
You shouldn't think like this in programming. Instead you should create a Car class that can represent any and all types of cars.
So maybe add a string variable called type, and insert that in the constructor fx. thereby declaring what particular type of car it is. Then simply call your class Car.
If you then later want to add methods to specific types of cars, you could make subclasses of your car class that extends the main car class.
Fx. like below. Then you add the additional methods a racing car should have to this class.
RacingCar : Car {
// class content
}
I am getting an error on lines 44 and 50.
It says:
No overload for method 'GetCarData' takes 1 arguments
Alright, so my task is to create an application that displays 3 main features: year, make, and speed of a car. The year and make are inputted with textboxes and the speed starts at 0.
There is an accelerate button which is supposed to add 5 to the speed every time it is pressed and a brake button which decreases the speed by 5 every time it is pressed.
I am having trouble using the class and form together to display the results. I need to display in a messagebox the make, year, and speed. I have been sitting here for hours and I am getting nowhere.
Any and all help is much appreciated. I have never worked with classes before.
Here is the form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Car_Class_BBrantley
{
public partial class Form1 : Form
{
private Car myCar;
public Form1()
{
myCar = new Car();
InitializeComponent();
}
private void GetCarData()
{
try {
myCar.Make = txtMake.Text;
myCar.Year = int.Parse(txtModel.Text);
myCar.Speed = 0;
}
catch (Exception ex)
{
MessageBox.Show(string.Concat("Must enter a valid make and year model for the car. ", ex.Message, "\r\n", ex.StackTrace));
}
}
private void btnAcc_Click(object sender, EventArgs e)
{
GetCarData();
myCar.AccSpeed(5);
MessageBox.Show(" Your car is a " + myCar.Year + myCar.Make + " and it is traveling " + myCar.Speed + " mph. ");
}
private void btnBrake_Click(object sender, EventArgs e)
{
GetCarData();
myCar.DecSpeed(5);
MessageBox.Show(" Your car is a " + myCar.Year + myCar.Make + " and it is traveling " + myCar.Speed + " mph. ");
}
}
}
If you would like to see the class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Car_Class_BBrantley
{
class Car
{
private int year;
private string make;
private int speed;
public Car()
{
this.year = 1994;
this.make = "Ford";
this.speed = 0;
}
public Car(string make, int year, int speed)
{
this.year = year;
this.make = make;
this.speed = speed;
}
public string Make
{
get { return make; }
set { make = value; }
}
public int Year
{
get { return Year; }
set { Year = value; }
}
public int Speed
{
get { return speed; }
set { speed = value; }
}
public void AccSpeed(int speedIncrement)
{
//Add check for speed limit ranges
Speed += speedIncrement;
}
public void DecSpeed(int speedDecrement)
{
//Add check for speed limit ranges
Speed -= speedDecrement;
}
}
}
If the error says
No overload for method 'GetCarData' takes 1 arguments
then look at the calls you make to GetCarData and check that they don't try to pass a parameter (argument) to it. It's declared as
private void GetCarData()
which is good, and the two calls (lines 44 and 50) are consistent with that:
GetCarData();
so make sure that the code on your computer matches what's here, save it, and re-compile.
I am getting a new error after having added the () to the Car instantiation.
It says:
'Car_Class_BBrantley.Form1.myCar()' must declare a body because it is not marked abstract, extern, or partial
The error is thrown on line 14.
Alright, so my task is to create an application that displays 3 main features: year, make, and speed of a car. The year and make are inputted with textboxes and the speed starts at 0.
There is an accelerate button which is supposed to add 5 to the speed every time it is pressed and a brake button which decreases the speed by 5 every time it is pressed.
I am having trouble using the class and form together to display the results. I need to display in a messagebox the make, year, and speed. I have been sitting here for hours and I am getting nowhere.
Any and all help is much appreciated. I'm sorry if this is a mess. I have never worked with classes before.
Here is the form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Car_Class_BBrantley
{
public partial class Form1 : Form
{
private Car myCar();
public Form1()
{
myCar = new Car();
InitializeComponent();
}
private void GetCarData(Car car)
{
try {
myCar.Make = txtMake.Text;
myCar.Year = int.Parse(txtModel.Text);
myCar.Speed = 0;
}
catch (Exception ex)
{
MessageBox.Show(string.Concat("Must enter a valid make and year model for the car. ", ex.Message, "\r\n", ex.StackTrace));
}
}
private void btnAcc_Click(object sender, EventArgs e)
{
GetCarData();
myCar.AccSpeed(5);
MessageBox.Show(" Your car is a " + myCar.Year + myCar.Make + " and it is traveling " + myCar.Speed + " mph. ");
}
private void btnBrake_Click(object sender, EventArgs e)
{
GetCarData();
myCar.DecSpeed(5);
MessageBox.Show(" Your car is a " + myCar.Year + myCar.Make + " and it is traveling " + myCar.Speed + " mph. ");
}
}
}
If you would like to see the class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Car_Class_BBrantley
{
class Car
{
private int year;
private string make;
private int speed;
public Car()
{
this.year = 1994;
this.make = "Ford";
this.speed = 0;
}
public Car(string make, int year, int speed)
{
this.year = year;
this.make = make;
this.speed = speed;
}
public string Make
{
get { return make; }
set { make = value; }
}
public int Year
{
get { return Year; }
set { Year = value; }
}
public int Speed
{
get { return speed; }
set { speed = value; }
}
public void AccSpeed(int speedIncrement)
{
//Add check for speed limit ranges
Speed += speedIncrement;
}
public void DecSpeed(int speedDecrement)
{
//Add check for speed limit ranges
Speed -= speedDecrement;
}
}
}
Change
private Car myCar();
to
private Car myCar;
and
private void GetCarData(Car car)
to
private void GetCarData()
change
myCar = new Car;
to myCar = new Car();