2 Questions
-
I trying to learn C# and I've got two questions for someone. :confused: 1) In my property statements when I have a speed property I keep getting told The type 'CarProjects.Car' already contains a definition for 'Speed'. I didn't think that I really had a definition for Speed in there. Hopefully someone can explain what I may be doing wrong. :confused: 2) In the Decelerate() method I need to take the final speed from accelerate and use it to set up for my braking speed. The way it is now I'm fine with the accelerate which gives me a 25 MPH boost over the initial speed. I can also get a -25 MPH shown in the decelerate. I had originally thought that I could make a method that would add the "brake" speed to the "speed" method. I simpply can't get it to work. Can someone give me a clue as to how I can make this work? :sigh: Any help will be appreciated.:)
public class Car private int speed = 0; private int brake = 0; private int Accel = 0; private int Speed = 0; //Property statment public int Speed { get { return speed; } set { speed = value; } } public int Brake { get { return brake; } set { brake = value; } } public Car(int speed, int brake, int accelerate) { accelerate = Accel; brake = Brake; speed = Speed; } public static int Accelerate() { int speed = 0; int counter = 1; Console.WriteLine(); Console.WriteLine("Type in the initial speed of car."); string inputNumbers = Console.ReadLine(); speed = int.Parse(inputNumbers); while (counter < 6) { speed += 5; counter++; } Console.WriteLine(); Console.WriteLine("Top speed of the car is: {0}", speed); return speed; } } public static int Decelerate() { int brake = 0; int counter = 1; while (counter < 6) { brake += - 5; counter++; } Console.WriteLine(); Console.WriteLine("Speed after braking is: {0}
-
I trying to learn C# and I've got two questions for someone. :confused: 1) In my property statements when I have a speed property I keep getting told The type 'CarProjects.Car' already contains a definition for 'Speed'. I didn't think that I really had a definition for Speed in there. Hopefully someone can explain what I may be doing wrong. :confused: 2) In the Decelerate() method I need to take the final speed from accelerate and use it to set up for my braking speed. The way it is now I'm fine with the accelerate which gives me a 25 MPH boost over the initial speed. I can also get a -25 MPH shown in the decelerate. I had originally thought that I could make a method that would add the "brake" speed to the "speed" method. I simpply can't get it to work. Can someone give me a clue as to how I can make this work? :sigh: Any help will be appreciated.:)
public class Car private int speed = 0; private int brake = 0; private int Accel = 0; private int Speed = 0; //Property statment public int Speed { get { return speed; } set { speed = value; } } public int Brake { get { return brake; } set { brake = value; } } public Car(int speed, int brake, int accelerate) { accelerate = Accel; brake = Brake; speed = Speed; } public static int Accelerate() { int speed = 0; int counter = 1; Console.WriteLine(); Console.WriteLine("Type in the initial speed of car."); string inputNumbers = Console.ReadLine(); speed = int.Parse(inputNumbers); while (counter < 6) { speed += 5; counter++; } Console.WriteLine(); Console.WriteLine("Top speed of the car is: {0}", speed); return speed; } } public static int Decelerate() { int brake = 0; int counter = 1; while (counter < 6) { brake += - 5; counter++; } Console.WriteLine(); Console.WriteLine("Speed after braking is: {0}
-
I trying to learn C# and I've got two questions for someone. :confused: 1) In my property statements when I have a speed property I keep getting told The type 'CarProjects.Car' already contains a definition for 'Speed'. I didn't think that I really had a definition for Speed in there. Hopefully someone can explain what I may be doing wrong. :confused: 2) In the Decelerate() method I need to take the final speed from accelerate and use it to set up for my braking speed. The way it is now I'm fine with the accelerate which gives me a 25 MPH boost over the initial speed. I can also get a -25 MPH shown in the decelerate. I had originally thought that I could make a method that would add the "brake" speed to the "speed" method. I simpply can't get it to work. Can someone give me a clue as to how I can make this work? :sigh: Any help will be appreciated.:)
public class Car private int speed = 0; private int brake = 0; private int Accel = 0; private int Speed = 0; //Property statment public int Speed { get { return speed; } set { speed = value; } } public int Brake { get { return brake; } set { brake = value; } } public Car(int speed, int brake, int accelerate) { accelerate = Accel; brake = Brake; speed = Speed; } public static int Accelerate() { int speed = 0; int counter = 1; Console.WriteLine(); Console.WriteLine("Type in the initial speed of car."); string inputNumbers = Console.ReadLine(); speed = int.Parse(inputNumbers); while (counter < 6) { speed += 5; counter++; } Console.WriteLine(); Console.WriteLine("Top speed of the car is: {0}", speed); return speed; } } public static int Decelerate() { int brake = 0; int counter = 1; while (counter < 6) { brake += - 5; counter++; } Console.WriteLine(); Console.WriteLine("Speed after braking is: {0}
JMOdom wrote:
- In my property statements when I have a speed property I keep getting told The type 'CarProjects.Car' already contains a definition for 'Speed'. I didn't think that I really had a definition for Speed in there. Hopefully someone can explain what I may be doing wrong.
I run into this problem when I accidently give a field (or method) the same name as a property. For example, if you have a field named "Speed" and a property named "Speed," you'll get the above error.
JMOdom wrote:
- In the Decelerate() method I need to take the final speed from accelerate and use it to set up for my braking speed. The way it is now I'm fine with the accelerate which gives me a 25 MPH boost over the initial speed. I can also get a -25 MPH shown in the decelerate. I had originally thought that I could make a method that would add the "brake" speed to the "speed" method. I simpply can't get it to work. Can someone give me a clue as to how I can make this work?
I think I need a clearer statement of what your goal is with the brake method. You're simulating a car, right? You have an Accelerate and Decelerate methods for increasing a car's speed and decreasing a cars speed respectively. Maybe a restatement of what you're having problems with would help. I'm a little lost.
-
JMOdom wrote:
- In my property statements when I have a speed property I keep getting told The type 'CarProjects.Car' already contains a definition for 'Speed'. I didn't think that I really had a definition for Speed in there. Hopefully someone can explain what I may be doing wrong.
I run into this problem when I accidently give a field (or method) the same name as a property. For example, if you have a field named "Speed" and a property named "Speed," you'll get the above error.
JMOdom wrote:
- In the Decelerate() method I need to take the final speed from accelerate and use it to set up for my braking speed. The way it is now I'm fine with the accelerate which gives me a 25 MPH boost over the initial speed. I can also get a -25 MPH shown in the decelerate. I had originally thought that I could make a method that would add the "brake" speed to the "speed" method. I simpply can't get it to work. Can someone give me a clue as to how I can make this work?
I think I need a clearer statement of what your goal is with the brake method. You're simulating a car, right? You have an Accelerate and Decelerate methods for increasing a car's speed and decreasing a cars speed respectively. Maybe a restatement of what you're having problems with would help. I'm a little lost.
I'll try and clear things up. :laugh: With this project that I'm working on I should be able to input an initial speed. The program will then take and add 5 MPH to it 5 times. When I get that final speed from the acceleration I then take it and use it in the decelerate method. It does the same thing with the following exception: Instead of adding 5 MPH to the speed it subtracts 5 MPH 5 times. I need to get back my original speed that I typed in. Hopefully that helps clear things up. :-D
-
I'll try and clear things up. :laugh: With this project that I'm working on I should be able to input an initial speed. The program will then take and add 5 MPH to it 5 times. When I get that final speed from the acceleration I then take it and use it in the decelerate method. It does the same thing with the following exception: Instead of adding 5 MPH to the speed it subtracts 5 MPH 5 times. I need to get back my original speed that I typed in. Hopefully that helps clear things up. :-D
Ok, the main thing is that you need to maintain the state of the car during and between calls to
Accelerate
andDecelerate
. In other words, you need to keep track of the speed of the car during and between calls to those methods. You can do this with a speed field. This is a trivial example, but it should show you in principle what I'm talking about:using System;
namespace Demo
{
public class Counter
{
private int count;public Counter(int initialCount) { count = initialCount; } public void Increment() { count++; } public void Decrement() { count--; } public int Count { get { return count; } } }
}
To use this object, we could do this:
Counter c = new Counter(40);
c.Increment();
c.Increment();
c.Decrement();// etc...
Now, hopefully this will get you started. In place of the
Increment
method, you will use theAccelerate
method. And instead of theDecrement
method, you will use theDecelerate
method. And instead of theCount
property, you will use theSpeed
property, and so on. You will then need to put in extra logic to handle your requirements regarding maximum speed allowed and so forth. The key here is that encapsulated in theCounter
object is the state of the object. This state is maintained between calls to the methods. I'm not sure, but I think this may have been where you were having trouble with your code. I'd need to take a second look. -
I trying to learn C# and I've got two questions for someone. :confused: 1) In my property statements when I have a speed property I keep getting told The type 'CarProjects.Car' already contains a definition for 'Speed'. I didn't think that I really had a definition for Speed in there. Hopefully someone can explain what I may be doing wrong. :confused: 2) In the Decelerate() method I need to take the final speed from accelerate and use it to set up for my braking speed. The way it is now I'm fine with the accelerate which gives me a 25 MPH boost over the initial speed. I can also get a -25 MPH shown in the decelerate. I had originally thought that I could make a method that would add the "brake" speed to the "speed" method. I simpply can't get it to work. Can someone give me a clue as to how I can make this work? :sigh: Any help will be appreciated.:)
public class Car private int speed = 0; private int brake = 0; private int Accel = 0; private int Speed = 0; //Property statment public int Speed { get { return speed; } set { speed = value; } } public int Brake { get { return brake; } set { brake = value; } } public Car(int speed, int brake, int accelerate) { accelerate = Accel; brake = Brake; speed = Speed; } public static int Accelerate() { int speed = 0; int counter = 1; Console.WriteLine(); Console.WriteLine("Type in the initial speed of car."); string inputNumbers = Console.ReadLine(); speed = int.Parse(inputNumbers); while (counter < 6) { speed += 5; counter++; } Console.WriteLine(); Console.WriteLine("Top speed of the car is: {0}", speed); return speed; } } public static int Decelerate() { int brake = 0; int counter = 1; while (counter < 6) { brake += - 5; counter++; } Console.WriteLine(); Console.WriteLine("Speed after braking is: {0}
On the fifth line you declare Seed two lines later you declare the property Speed with the same name. It is a dangerous habit to use speed and Speed in the same module. For properties I normally create a private var like this PropSpeed, then the property itself has the more friendly name Speed. I think your model is nog good. If you intend to use Speed as the actual seepd an you simulate braking, just decrease the speed again, till it treaches 0. You do not need the brake proerty for this purpose. It might make sens to introcuce properties for accelleration to set the accelleration capability and use this to calculate the new speed: speed += accelleration; In the same way you may want to use a different value for brakng capacitiy, e.g. brake: speed -= brake; I hope this helps.
-
On the fifth line you declare Seed two lines later you declare the property Speed with the same name. It is a dangerous habit to use speed and Speed in the same module. For properties I normally create a private var like this PropSpeed, then the property itself has the more friendly name Speed. I think your model is nog good. If you intend to use Speed as the actual seepd an you simulate braking, just decrease the speed again, till it treaches 0. You do not need the brake proerty for this purpose. It might make sens to introcuce properties for accelleration to set the accelleration capability and use this to calculate the new speed: speed += accelleration; In the same way you may want to use a different value for brakng capacitiy, e.g. brake: speed -= brake; I hope this helps.
:-D Thanks folks. I'll be trying the various solutions to see which gives me the best results. :-O As I'm new at this, and in a learning mode, sometimes I find that you learn more from how not to do something than from how to do it. :doh: Thanks again for your time and efforts. They are all appreciated. :cool::rose: