Reputation: 25
I've just started to work with Windows Form and there's something I can't figure out. I have two radiobuttons on the form that are listed to the same method. The method looks like this, and it is in a class other than the MainForm.cs:
public double CalcBMR()
{
double bmr = 0.0;
if (isFemale)
bmr = (10 * weight + 6.25 * height - 5 * age) - 161;
else
bmr = (10 * weight + 6.25 * height - 5 * age) + 5;
return bmr;
}
So, what I want to do is telling the program that rbtnFemale.Checked = isFemale = true, and that rbtnMale.Checked = isFemale = false, but how do I do that considering that the isFemale bool has to be private (and in another class) and that I cannot write one different method for each option? This is what I've tried but apparently it doesn't work:
private void rbtnFemale_CheckedChanged(object sender, EventArgs e)
{
bool isFemale = true;
bmrCalc.CalcBMR();
}
Or also this:
private void rbtnFemale_CheckedChanged(object sender, EventArgs e)
{
bool isFemale = true;
bmrCalc.CalcBMR(isFemale);
}
WHich of course doesn't work because "No overload for method CalcBMR() takes 1 argument". I don't know what to do. Thank you in advance
Upvotes: 0
Views: 55
Reputation: 5353
So, what I want to do is telling the program that rbtnFemale.Checked = isFemale = true
Then do so, by substituting this expression for the old isFemale
variable.
public double CalcBMR()
{
double bmr = 0.0;
if (rbtnFemale.Checked)
bmr = (10 * weight + 6.25 * height - 5 * age) - 161;
else
bmr = (10 * weight + 6.25 * height - 5 * age) + 5;
return bmr;
}
If you don't want your CalcBMR
function to rely on UI elements, have it accept a bool isFemale
instead.
public double CalcBMR(bool isFemale)
{
double bmr = 0.0;
if (isFemale)
bmr = (10 * weight + 6.25 * height - 5 * age) - 161;
else
bmr = (10 * weight + 6.25 * height - 5 * age) + 5;
return bmr;
}
and call it with the appropiate argument
private void rbtnFemale_CheckedChanged(object sender, EventArgs e)
{
bmrCalc.CalcBMR(rtbnFemale.Checked);
}
Upvotes: 3
Reputation: 107
Upvotes: 0