Method & Class Issue

I have a form called Add.

Some of the fields include First Name and Last Name.

When I click on the Add button, it will call the Add method in a class called Add which will add 7 to a number. It does not do the add calculation here. If I enter in 10, 10 will be returned and not 17. What am I doing wrong?




   

   
		private string _firstName;
        private string _lastName;
        private int _add;
     public Add()
        {
            _firstName = "";
            _lastName = "";

            _add = 0;
        }
		        public void Add()
        {
            _add = _add + 5;

        }

Open in new window

           
            
		        private void addButton_Click(object sender, EventArgs e)
        {
            add addData = new add();
            addData.Add();
            GetAddData(addData);

            calculatedAnswer.Text = addData.Add.ToString();

        }

Open in new window

LVL 3
Computer GuyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

plusone3055Commented:
what your doing is not adding the number  you need to s
AndyAinscowFreelance programmer / ConsultantCommented:
Please post the complete code - you are missing some functionality.
Computer GuyAuthor Commented:
namespace CarClass
{
    class Car
    {
        private int _year;
        private string _make;
        private int _speed;

    

        public Car(int speed)
        {
            _year = Year;
            _make = "";

            _speed = speed;
            speed = 0;
        }

        public int Year
        {
            get { return _year; }
            set { _year = value; }
        }

        public string Make
        {
            get { return _make; }
            set { _make = value; }
        }

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

        public void Accelerate(int speed)
        {
            _speed = speed + 7;

        }

        public int Brake
        {
            get { return _speed -7 ; }
        }


    }
}

Open in new window


       public Form1()
        {
            InitializeComponent();
        }

        private void GetCarData(Car carData)
        {
            int year;

            carData.Make = makeTextBox.Text;

            if (int.TryParse(yearTextBox.Text, out year))
            {
                carData.Year = year;
            }
            else
            {
                MessageBox.Show("Invalid Year");
            }

        }


        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void accelerateButton_Click(object sender, EventArgs e)
        {

                Car carData = new Car();
                carData.Accelerate();
                GetCarData(carData);

                outputYear.Text = carData.Year.ToString();
                outputMake.Text = carData.Make;
                outputSpeed.Text = carData.Speed.ToString();
            

        }

Open in new window

Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Computer GuyAuthor Commented:
Hi,

Lets stick with this code for now.

2 problems with it:

I get CarClass.CarInfo.Speed: property or indexer must have at least one accessor

Also, I had it working at one point (not sure what I did to mess it up) and then when I put 10 in the textbox, it will add 7, but not continue to add 7 if I keep clicking on it.

CarClass
namespace CarClass
{
    class CarInfo
    {
        private int _speed;

        public CarInfo(int speed)
        {
            _speed = speed;
        }

        public int Speed
        {
            get { return _speed;  }
        }

        public void Accelerate(int speed)
        {
            
            _speed = speed + 7;
        }
        public void Brake(int speed)
        {
            _speed = speed - 7;
        }

    }

Open in new window


Form
 public partial class Form1 : Form
    {

        private CarInfo data = new CarInfo(0);
        
        public Form1()
        {
            InitializeComponent();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

       
        private void accelerateButton_Click(object sender, EventArgs e)
        {
            int speed;

            if (int.TryParse(speedTextBox.Text, out speed))
            {
                data.Accelerate(speed);

                outputSpeed.Text = data.Speed.ToString();
            }

        }
        

        private void brakeButton_Click(object sender, EventArgs e)
        {
            int speed;

            if (int.TryParse(speedTextBox.Text, out speed))
            {
                data.Brake(speed);

                outputSpeed.Text = data.Speed.ToString();
            }

        }
    }
}

Open in new window

AndyAinscowFreelance programmer / ConsultantCommented:
>>I get CarClass.CarInfo.Speed: property or indexer must have at least one accessor
at which line?



>>but not continue to add 7 if I keep clicking on it.
        public void Accelerate(int speed)
        {
            
            _speed = speed + 7;
        }
        public void Brake(int speed)
        {
            _speed = speed - 7;
        }

Open in new window


If you look very closely at the underlines I suspect you wanted to use the _speed not speed variable in the addition
Computer GuyAuthor Commented:
Thanks!

I this works, though it does not take the value I entered in the textbox on the form and add it to 7.

Say I enter 10, it would be 10 + 7 and add + 7 everytime I click the button.

17, 24, 31, etc...
AndyAinscowFreelance programmer / ConsultantCommented:
What did you change (if anything) in response to my previous comment.  I think you needed to make a change but wasn't certain as I didn't really understand what you wanted.  From your last comment I am now certain you needed to change your code by adding an underline.
Computer GuyAuthor Commented:
Sorry, I did do that.

When I click on Accelerate it does not take the value in the textbox and add it to 7. It just shows 7, 14, 21.

Ideally, if I enter  100 in the textbox it should display 107.
AndyAinscowFreelance programmer / ConsultantCommented:
Aha.  I think your original code was correct after all:
       public void Accelerate(int speed)
        {
            
            _speed = speed + 7;
        }
        public void Brake(int speed)
        {
            _speed = speed - 7;
        }

Open in new window

Computer GuyAuthor Commented:
when I click add with my original code it does add but when I keep clicking accelerate it does not keep adding 7.
AndyAinscowFreelance programmer / ConsultantCommented:
I'm confused.
From reading your comments it seems like you want to do two different things with the one function.
Give me a few minutes and I'll throw some code together.
AndyAinscowFreelance programmer / ConsultantCommented:
    public class Vehicle
    {
        private Vehicle() { }
        protected Vehicle(string name, int delta) { _Name = name; _Delta = delta;  }

        private String _Name;
        public string Name
        {
            get { return _Name; }
        }

        private int _Speed = 0;
        public int Speed
        {
            get { return _Speed; }
            set { _Speed = value; }     //Ought to have a check for negative speed !
        }

        private int _Delta = 0;

        public void Accelerate()
        {
            _Speed += _Delta;
        }
    }

    public class Car : Vehicle
    {
        public Car() : base("Car", 7) { }
    }

    public class Motorcycle : Vehicle
    {
        public Motorcycle() : base("Motorcycle", 12) { }    //because bikes typically have better acceleration
    }

Open in new window


Usage:
Car carOne = new Car();
Motorcycle motorcycleOne = new Motorcycle();
....


carOne.Speed = 20;  //Set the speed (from the textbox)

In response to the accelerate button
carOne.Accelerate();  //should now be 27 - note this does NOT set the initial speed
carOne.Accelerate();  //Now 34

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.