I'm trying to test if a user input is numeric, and if the input is not numeric, a little message box pops up and alerts the user. (I've done this part). The issue I'm having is the and error continues to pop in visual studio
Is this a formatting issue? Should I reformat my code? As you can probably see I am new the C# (and programming).
My code:
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 wtgCalculator {
public partial class Form1 : Form {
const double maleratio = 0.536;
const double femaleratio = 0.492;
public Form1() {
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e) {
}
private void Form1_Load(object sender, EventArgs e) {
}
private void label1_Click(object sender, EventArgs e) {
}
private void textBox1_TextChanged_1(object sender, EventArgs e) {
}
private void WaistNumericCheck() {
double waist;
string inpwaist = heighttextbox.Text;
if (double.TryParse(inpwaist, out waist)) {
MessageBox.Show('Value must be numeric'); //this is where the problem is
}
}
private void HeightNumericCheck() {
//todo
}
private void button1_Click(object sender, EventArgs e) {
//record inputs to variables
WaistNumericCheck();
HeightNumericCheck();
double height = double.Parse(heighttextbox.Text);
double waist = double.Parse(waisttextbox.Text);
//check is inputs are within boundry
CheckDimensions(height, waist);
//test
// ShowResult(height, waist);
}
private void CheckLimits(double height, double waist) {
double result = CalculateRatio(height, waist);
if (Female.Checked) {
if (result < femaleratio) {
MessageBox.Show('Your risk of obesity related cardiovasular is low');
}
if (result > femaleratio) {
MessageBox.Show('Your risk of obesity related to cardiovascular is high');
}
}
if (Male.Checked) {
if (result < maleratio) {
MessageBox.Show('Your risk of obesity related cardiovasular is low');
}
if (result > maleratio) {
MessageBox.Show('Your risk of obesity related cardiovasular is High');
}
}
//testing
MessageBox.Show(result.ToString());
}
private void ShowResult(double height, double waist) {
//double result = CalculateRatio(height, waist);
//if (Female.Checked) {
// if (result < femaleratio) {
// MessageBox.Show('Your risk of obesity related cardiovasular is low');
// }
// if (result > femaleratio) {
// MessageBox.Show('Your risk of obesity related to cardiovascular is high');
// }
//}
//if (Male.Checked) {
//}
}
private static void CheckDimensions(double height, double waist) {
if (height <= 120) {
MessageBox.Show('Height must be greater than 120cm');
}
if (waist <= 60) {
MessageBox.Show('Waist must be greater than 60cm');
}
}
private void Gender_CheckedChanged(object sender, EventArgs e) {
button1.Enabled = true;
}
private static double CalculateRatio(double height, double waist) {
double finalratio = waist / height;
return finalratio;
}
}
}
Anonymous User
15-Nov-2014You're not really handling this correctly. .Parse() is supposed to throw an exception if the value cannot be converted. The whole point of .TryParse() is to avoid that. So, your methods need to be cleaned up and edited to actually accomplish something:
This modifies your test methods to return a true/false based on whether the parse was successful, and outs your parsed variables so that you don't have to parse them again.