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.
Related
I'm learning C#, trying to get to grips with accessors at the moment.
I'm going nuts looking at this, I have no idea what I've done wrong:
class BankAccount
{
// *PROPERTIES*
private int _initialDeposit = 0;
// **ACCESSORS**
public int SavingsAccount
{
set
{
_initialDeposit = value;
}
get
{
return _initialDeposit;
}
}
}
The Form looks like this:
public partial class BankForm : Form
{
private BankAccount _myAccount;
public BankForm()
{
InitializeComponent();
_myAccount = new BankAccount();
}
private void initialDepositButton_Click(object sender, EventArgs e)
{
_myAccount.SavingsAccount = Convert.ToInt32(initialDepositTextBox.Text);
bankAccountListBox.Text = "Account opened with initial Deposit " + initialDepositTextBox.Text;
}
}
But I get this error:
Property or indexer must have at least one accessor
I'm not getting any errors. Move location of private BankAccount _myAccount;
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 BankForm
{
public partial class BankForm : Form
{
public BankForm()
{
InitializeComponent();
_myAccount = new BankAccount();
}
private BankAccount _myAccount;
private void initialDepositButton_Click(object sender, EventArgs e)
{
_myAccount.SavingsAccount = Convert.ToInt32(initialDepositTextBox.Text);
bankAccountListBox.Text = "Account opened with initial Deposit " + initialDepositTextBox.Text;
}
}
class BankAccount
{
// *PROPERTIES*
private int _initialDeposit = 0;
// **ACCESSORS**
public int SavingsAccount
{
set
{
_initialDeposit = value;
}
get
{
return _initialDeposit;
}
}
}
}
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.
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.
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();
I am trying to learn classes, meaning writing codes outside of the windows form and calling that code in the form. I created a basic calculation in the form and it works. The problem is, I want to know how I can move the entire calculation into a class and call it into the form on load or when a control is click. It does not work!
Here is my local or form version which works:
Please note that the form will load with the first calculation because the Radiobutton is checked on load.
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 Calculations
{
public partial class Calculations : Form
{
public Calculations()
{
InitializeComponent();
}
public decimal SalesTax = 0.065M;
public decimal AppliedTax;
public decimal Price;
public decimal SubTotal;
public decimal GrandTotal;
public object Calc()
{
Int32 GetQuantity = Convert.ToInt32(txtQuantity.Text);
SubTotal = (Price * GetQuantity);
AppliedTax = (SubTotal * SalesTax);
GrandTotal = (SubTotal + AppliedTax);
if (radRed.Checked == true)
{
Price = 100;
}
else if (radBlue.Checked == true)
{
Price = 200;
}
else if (radGreen.Checked == true)
{
Price = 300;
}
lblPrice.Text = GrandTotal.ToString("c");
return GrandTotal;
}
private void Calculations_Load(object sender, EventArgs e)
{
txtQuantity.Text = "10";
radRed.Checked = true;
Calc();
}
private void radRed_CheckedChanged(object sender, EventArgs e)
{
Calc();
}
private void radBlue_CheckedChanged(object sender, EventArgs e)
{
Calc();
}
private void radGreen_CheckedChanged(object sender, EventArgs e)
{
Calc();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
Calc();
}
}
}
And here is my attempt to place all of this in a class:
It works but not on load, you have to check another radiobutton first
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Calculations
{
class ClsCalc
{
class ClsRef
{
public static Calculations FormCtrls
{
get
{
return Calculations.ActiveForm as Calculations;
}
}
}
public static decimal SalesTax = 0.065M;
public static decimal AppliedTax;
public static decimal Price;
public static decimal SubTotal;
public static decimal GrandTotal;
public static object Calc()
{
Int32 GetQuantity = Convert.ToInt32(ClsRef.FormCtrls.txtQuantity.Text);
SubTotal = (Price * GetQuantity);
AppliedTax = (SubTotal * SalesTax);
GrandTotal = (SubTotal + AppliedTax);
if (ClsRef.FormCtrls.radRed.Checked == true)
{
Price = 100;
}
else if (ClsRef.FormCtrls.radBlue.Checked == true)
{
Price = 200;
}
else if (ClsRef.FormCtrls.radGreen.Checked == true)
{
Price = 300;
}
ClsRef.FormCtrls.lblPrice.Text = GrandTotal.ToString("c");
return GrandTotal;
}
}
}
Finally I called it in the form Load event:
ClsCalc.Calc();
Please note this is not a real project just a way to learn, well for me that is.
Thank you!
Your calculation method is almost entirely involving interactions with UI elements. You gather a bunch of information from a textbox and various checkboxes, and then have just three lines of actual calculations, and then you display the results.
UI interaction shouldn't be moved outside of the form. You shouldn't expose the internal controls of a form publicly; they should only ever be accessed from within their parent form (or user control, if that's the case).
A common pattern that you'll see when performing some form submit operation is to gather information from input controls, do some calculations on the data, and then display the results on the form. Both the gathering information and displaying results should stay within the form, the only thing that you should (potentially) move outside of the form's definition is the calculations. In this case that's such a small amount of work there just isn't a compelling reason to do that.