15,614,766 members
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.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()
{
InitializeComponent();
}

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

Calculate();
}
}
}

What I have tried:

Moving the below code inside the method Calculate:

Posted
Updated 27-Jul-21 0:46am

## Solution 2

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:
C#
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);
txtMass.Focus();
return;
}

## Solution 1

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:
C#
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.

v2
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.
:laugh:
Richard MacCutchan 27-Jul-21 6:23am
I don't think this calculator is likely to be used by NASA. :)