-3

I am writing a program that calculates the C side of a triangle using Pythagoras' theorem. I have to use a class.

Why does it give 0 as an answer with the following code: What is the solution:

code in class:

    public class Pyt
    {
        public Pyt(double rh1, double rh2, double _Som, double _Som2)
        {
            _Rh1 = rh1;
            _Rh2 = rh2;
        }

        public double _Rh1;
        public double _Rh2;

        public double _Som()
        {
            return _Rh1 * _Rh1 + _Rh2 * _Rh2;
        }

        public double _Som2()
        {
            return Math.Sqrt(_Som());
        }
    }
}

code under button:

  private void button1_Click(object sender, EventArgs e)
        {
            double rhzijde1 = 0;
            double rhzijde2 = 0;
            double _Som2 = 0;
            double _Som = 0;
           
            rhzijde1 = double.Parse(textBox1.Text);
            rhzijde2 = double.Parse(textBox2.Text);
           
            textBox3.Text = _Som2.ToString();
        }
    }
}

Without use of a class it works.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Stefan N
  • 53
  • 7
  • 1
    You are using `_Som2.ToString()` where `_Som2 = 0`. You might want to use the `Pyt`. – SGKoishi Dec 22 '22 at 20:42
  • 4
    This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Dec 22 '22 at 20:42

1 Answers1

1

If you want to work with class Pyt just do it:

double rhzijde1 = 0;
double rhzijde2 = 0;
double _Som2 = 0;
double _Som = 0;
           
rhzijde1 = double.Parse(textBox1.Text);
rhzijde2 = double.Parse(textBox2.Text);

// Let's use the class: 
//   1. Create Pyt instance
Pyt pyt = new Pyt(rhzijde1, rhzijde2, 0, 0);

//   2. Compute with a help of the instance created
_Som = pyt.Som();
_Som2 = pyt.Som2();

textBox3.Text = _Som2.ToString();
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Thanks, Indeed it was the part I forgot to do. When creating a class you have to mention it again under button.. – Stefan N Dec 23 '22 at 01:42