Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. 2 Questions

2 Questions

Scheduled Pinned Locked Moved C#
csharpperformancehelpquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JMOdom
    wrote on last edited by
    #1

    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}

    L L R 3 Replies Last reply
    0
    • J JMOdom

      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}

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      JMOdom wrote:

      private int Speed = 0;

      JMOdom wrote:

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

      led mike

      1 Reply Last reply
      0
      • J JMOdom

        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}

        L Offline
        L Offline
        Leslie Sanford
        wrote on last edited by
        #3

        JMOdom wrote:

        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.

        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:

        1. 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.

        J 1 Reply Last reply
        0
        • L Leslie Sanford

          JMOdom wrote:

          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.

          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:

          1. 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.

          J Offline
          J Offline
          JMOdom
          wrote on last edited by
          #4

          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

          L 1 Reply Last reply
          0
          • J JMOdom

            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

            L Offline
            L Offline
            Leslie Sanford
            wrote on last edited by
            #5

            Ok, the main thing is that you need to maintain the state of the car during and between calls to Accelerate and Decelerate. 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 the Accelerate method. And instead of the Decrement method, you will use the Decelerate method. And instead of the Count property, you will use the Speed 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 the Counter 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.

            1 Reply Last reply
            0
            • J JMOdom

              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}

              R Offline
              R Offline
              Rudolf Jan
              wrote on last edited by
              #6

              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.

              J 1 Reply Last reply
              0
              • R Rudolf Jan

                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.

                J Offline
                J Offline
                JMOdom
                wrote on last edited by
                #7

                :-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:

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups