如何使局部变量在其他方法中可用

时间:2019-10-20 19:39:05

标签: c# .net

我对C#相当陌生,我不知道如何使我的变量之一可以通过另一种方法访问。这是一个程序,它吸收食物中的卡路里和脂肪克,然后显示脂肪的百分比和脂肪的卡路里数量。然后有一个复选框询问您是否要查看它是否为低脂食品。选中后,我希望它获取脂肪百分比的结果并显示是否为低脂肪。但是由于decimal percentCalsFromFat是局部变量,因此在复选框代码下无法识别。我读到可以将其提升到类级别的范围,但是我不知道如何写出它以及将其确切放置在代码中以使其起作用。然后,它必须将结果显示在标签框的底部。这是代码:

这是应用程序完成后的外观:

enter image description here

private void CalculateButton_Click(object sender, EventArgs e)
{
    //local variables
    decimal totalCals;
    decimal fatGramsInFood;
    decimal calsFromFat;
    decimal percentCalsFromFat;
    //Get the calories and fat grams.
    totalCals = decimal.Parse(numOfCaloriesTextBox.Text);
    fatGramsInFood = decimal.Parse(numOfFatGramsTextBox.Text);
    //Determine whether the calories and fat grams are higher than 0.
    if (totalCals >= 0 && fatGramsInFood >= 0)
    {
        if (totalCals > fatGramsInFood)
        {
            //Calculate calories from fat.
            calsFromFat = fatGramsInFood * 9;
            //Display the calories from fat.
            resultsLabel.Text = calsFromFat.ToString();
            //Calculate percentage of calories from fat.
            percentCalsFromFat = calsFromFat / totalCals;
            //Display percentage of calories from fat.
            resultsLabel.Text = "The number of calories from fat:" + Environment.NewLine;
            resultsLabel.Text += "Percentage of calories from fat:" + percentCalsFromFat.ToString("p") + Environment.NewLine;
        }
        else
        {
            //Display an error message "calories must to be higher than fat grams".
            MessageBox.Show("Calories must be higher than fat grams");
        }
    }
    else
    {
        //Display an error message "calories and fat grams need be higher than 0".
        MessageBox.Show("Calories and fat grams need to be higher than 0");
    }
}

private void foodLowFatCheckBox_CheckedChanged(object sender, EventArgs e)
{
    decimal percentCalsFromFat;
    if (foodLowFatCheckBox.Checked && percentCalsFromFat <= 30)
    {
        resultsLabel.Text = "The food is considered low fat";
    }
    else
    {
        resultsLabel.Text = "The food is not considered low fat";
    }
}

1 个答案:

答案 0 :(得分:0)

您可以在任何地方使它可访问:

namespace Program4_10
{
    private double calsFromFat, percentCalsFromFat;
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            percentCalsFromFat = calsFromFat = 0;
        }
        private void CalculateButton_Click(object sender, EventArgs e)
        {
            //local variables
            double totalCals;
            double fatGramsInFood;
            //Get the calories and fat grams.
            totalCals = double.Parse(numOfCaloriesTextBox.Text);
            fatGramsInFood = double.Parse(numOfFatGramsTextBox.Text);
            //Determine whether the calories and fat grams are higher than 0.
            if (totalCals >= 0 && fatGramsInFood >= 0)
            {
                if (totalCals > fatGramsInFood)
                {
                    //Calculate calories from fat.
                    calsFromFat = fatGramsInFood * 9;
                    //Calculate percentage of calories from fat.
                    percentCalsFromFat = calsFromFat / totalCals;
                    DisplayText();
                }
                else
                {
                    //Display an error message "calories must to be higher than fat grams".
                    MessageBox.Show("Calories must be higher than fat grams");
                }
            }
            else
            {
                //Display an error message "calories and fat grams need be higher than 0".
                MessageBox.Show("Calories and fat grams need to be higher than 0");
            }
        }
        private void foodLowFatCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            DisplayText();
        }
        private void DisplayText()
        {
            //Display percentage of calories from fat.
            resultsLabel.Text = "The number of calories from fat:" + calsFromFat.ToString() + Environment.NewLine;
            resultsLabel.Text += "Percentage of calories from fat:" + percentCalsFromFat.ToString("p") + Environment.NewLine;
            if (foodLowFatCheckBox.Checked)
            {
                if (percentCalsFromFat <= 0.3)
                    resultsLabel.Text += "The food is considered low fat";
                else
                    resultsLabel.Text += "The food is not considered low fat";
            }
        }
    }
}
相关问题