Click here to Skip to main content
15,943,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have started to write some basic code, I basically want to calculate 3 variables and store them in another variable called "potentialEnergy". That will then convert to a string and display in a text box called txtAns. Below is what I have but I'm very new to this..

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 Incline_Impact

    public partial class Incline : Form

        double m; //mass in kg  
        double h; //height in meters
        double g; //gravity 9.81
        double u; //coefficient of friction, 0.18 steel-steel
        double angle; //9.92 angle for our machine
        double sl; //sensor length
        double potentialEnergy;

        public Incline()

        public double Calculate(double potentialEnergy) //work on this - how to return a value
            potentialEnergy = m * g * h;
            return potentialEnergy;

        public void btnCalculate_Click(object sender, EventArgs e)
            double defaultDouble = 0;
            bool m = double.TryParse(txtMass.Text, out defaultDouble);//mass in kg
            bool h = double.TryParse(txtHeight.Text, out defaultDouble);//height in meters
            bool g = double.TryParse(txtGravity.Text, out defaultDouble);//gravity 9.81 default

            string answer = Convert.ToString(potentialEnergy);
            txtAns.Text = answer;

What I have tried:

Moving the below code inside the method Calculate:

string answer = Convert.ToString(potentialEnergy);
txtAns.Text = answer;
Updated 27-Jul-21 0:46am

You have defined your Calculate as taking an input parameter of a double value, and returning another double value. But when you call it in the btnCalculate_Click method, you do not pass it any value, or capture its return. also, you are trying to convert three different text strings to double values, but you store each one in the same variable, thus losing the first two. You also ignore any errors which may occur if the text dstrings are not valid numbers. You need to rework your code and think exactly what each step is required to accomplish. For example, in the btnCalculate_Click method, you need to validate each input, and take some action if it is not a valid number, or outside some minimum and maximum range. You also need to store it in the correct variable read for the calculation. And your calculate method would be better defined thus:
public double Calculate(double mass, double height, double gravity)
    double potentialEnergy = mass * gravity * height;

    return potentialEnergy;

And unless you are calculating for different planets (or you are a real geek), the gravity value should be a constant.
Share this answer
OriginalGriff 27-Jul-21 6:08am    
"... unless you are calculating for different planets, the gravity value should be a constant."
Not necessarily - it varies on the surface of the Earth:
"Mount Nevado Huascarán in Peru has the lowest gravitational acceleration, at 9.7639 m/s2, while the highest is at the surface of the Arctic Ocean, at 9.8337 m/s2." - New Scientist
Not to mention that it reduces as you get further away from the centre.
Richard MacCutchan 27-Jul-21 6:23am    
I don't think this calculator is likely to be used by NASA. :)
Just to add to what Richard has said ...
When you use TryParse (or TryParseExact) methods - and you definitely should - you need to check the result, and show an error if it fails instead of just blindly continueing.

Something like this:
double mass= 0;
if (!double.TryParse(txtMass.Text, out mass))
    MessageBox.Show("The mass needs to a a number of Kg", "Input error", MessageBoxButtons.OK);
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900