CS0103-该名称在上下文中不存在

时间:2019-02-17 16:25:06

标签: c# visual-studio

我目前正在逐步学习Microsoft Visual C#第九版课程。我遇到一个问题,指出该名称在上下文中不存在。我进行了一些研究,找到了如何解决问题的示例,但仍然没有运气。我应该忽略它,继续还是做更多研究?

这是导致问题的脚本。

       (division, remainder) = divide(leftHandSide, rightHandSide);

代码:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    private void calculateClick(object sender, RoutedEventArgs e)
    {
        int calculatedValue = 0;

        try
        {
            int leftHandSide = System.Int32.Parse(lhsOperand.Text);
            int rightHandSide = System.Int32.Parse(rhsOperand.Text);


            if (addition.IsChecked.HasValue && addition.IsChecked.Value)
            {
                calculatedValue = addValues(leftHandSide, rightHandSide);
                showResult(calculatedValue);
            }
            else if (subtraction.IsChecked.HasValue && subtraction.IsChecked.Value)
            {
                calculatedValue = subtractValues(leftHandSide, rightHandSide);
                showResult(calculatedValue);
            }
            else if (multiplication.IsChecked.HasValue && multiplication.IsChecked.Value)
            {
                calculatedValue = multiplyValues(leftHandSide, rightHandSide);
                showResult(calculatedValue);
            }
            else if (division.IsChecked.HasValue && division.IsChecked.Value)
            {
                int division, remainder;
                (division, remainder) = divide(leftHandSide, rightHandSide);

                result.Text = $"{division} remainder {remainder}";
            }
        }
        catch (Exception caught)
        {
            expression.Text = "";
            result.Text = caught.Message;
        }
    }

    private int addValues(int leftHandSide, int rightHandSide)
    {
        expression.Text = $"{leftHandSide} + {rightHandSide}";
        return leftHandSide + rightHandSide;
    }

    private int subtractValues(int leftHandSide, int rightHandSide)
    {
        expression.Text = $"{leftHandSide} - {rightHandSide}";
        return leftHandSide - rightHandSide;
    }

    private int multiplyValues(int leftHandSide, int rightHandSide)
    {
        expression.Text = $"{leftHandSide} * {rightHandSide}";
        return leftHandSide * rightHandSide;
    }

    private int divideValues(int leftHandSide, int rightHandSide)
    {
        expression.Text = $"{leftHandSide} / {rightHandSide}";
        return leftHandSide / rightHandSide;
    }

    private int remainderValues(int leftHandSide, int rightHandSide)
    {
        expression.Text = $"{leftHandSide} % {rightHandSide}";
        return leftHandSide % rightHandSide;
    }

    private void showResult(int answer) => result.Text = answer.ToString();
}

1 个答案:

答案 0 :(得分:0)

在除法部分中,您正在调用divide函数,该函数应该返回具有两个值的元组。在代码中的任何地方都没有定义这样的方法。相反,您定义了divideValuesremainderValues。您可以将其更改为:

else if (division.IsChecked.HasValue && division.IsChecked.Value)
{
    int division = divideValues(leftHandSide, rightHandSide);
    int remainder = remainderValues(leftHandSide, rightHandSide);

    result.Text = $"{division} remainder {remainder}";
}

请注意,通过依次调用这些函数,您只会看到expression.Text中的remainderValues


此外,您还可以编写一个新功能:

private (int division, int remainder) divide(int leftHandSide, int rightHandSide)
{
    expression.Text = $"division: {leftHandSide} % {rightHandSide}, remainder: {leftHandSide} % {rightHandSide}";
    return (leftHandSide / rightHandSide, leftHandSide % rightHandSide);
}

然后像以前一样致电: