C# 并非所有代码路径都返回值数据收集器类项目

标签 c# methods return

我试图找出这段代码中的问题所在,但我找不到它。 声明开始....

    private Units unitsToUse;
    private int[] dataCaptured = new int[30];
    private int mostRecentMeasure;

语句末尾的方法....

    public int Measure(int v, Units u)
    {
        if (v != 0 && v != null)
        {
            mostRecentMeasure = v;
            if (u == Units.Metric)
            {
                unitsToUse = u;
                for (int i = 0; i < 30; i++)
                {
                    if (dataCaptured[i] != 0 && dataCaptured[i] != null && i < 29)
                    {
                        continue;
                    }
                    if (i == 29 && dataCaptured[i] != null)
                    {
                        i = 0;
                        dataCaptured[i] = mostRecentMeasure;
                        return mostRecentMeasure;
                    }
                    dataCaptured[i] = mostRecentMeasure;
                    return mostRecentMeasure;
                }
            }
            else if (u == Units.Imperial)
            {
                unitsToUse = u;
                for (int i = 0; i < 30; i++)
                {
                    if (dataCaptured[i] != 0 && dataCaptured[i] != null && i < 29)
                    {
                        continue;
                    }
                    if (i == 29 && dataCaptured[i] != null)
                    {
                        i = 0;
                        dataCaptured[i] = mostRecentMeasure;
                        return mostRecentMeasure;
                    }
                    dataCaptured[i] = mostRecentMeasure;
                    return mostRecentMeasure;
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException("Your units were neither of the available values.");
            }
        }
        else
        {
            throw new ArgumentOutOfRangeException("Your value of measuring was not in the specified range.");
        }
    }

现在,该方法将通过代码中其他位置的随机 1-10 方法接收单位的有效枚举值和 int V 的有效值。我不明白的是,代码没有返回值或抛出异常来处理在参数之外执行的方法的任何错误情况。我已经被困在这里有一段时间了,任何帮助将不胜感激。

最佳答案

首先,考虑重组逻辑控制语句以防止更少的嵌套。这使得更容易检测不返回的路径。

其次,您要检查值类型是否为 null,这是不正确的,并且评估永远不会为 true,因为值类型不能为 null。

第三,当满足条件时,您应该跳出循环并从循环外部返回,而不是尝试从循环内部返回。

public int Measure(int v, Units u)
{
    if (v == 0)
        throw new ArgumentOutOfRangeException(
            "Your value of measuring was not in the specified range.");

    mostRecentMeasure = v;
    if (u == Units.Metric)
    {
        unitsToUse = u;
        for (int i = 0; i < 30; i++)
        {
            if (dataCaptured[i] != 0 && i < 29)
            {
                continue;
            }
            if (i == 29)
            {
                i = 0;
                dataCaptured[i] = mostRecentMeasure;
                return mostRecentMeasure;
            }
            dataCaptured[i] = mostRecentMeasure;
            break;
        }

        return mostRecentMeasure;
    }
    else if (u == Units.Imperial)
    {
        unitsToUse = u;
        for (int i = 0; i < 30; i++)
        {
            if (dataCaptured[i] != 0 && i < 29)
            {
                continue;
            }
            if (i == 29)
            {
                i = 0;
                dataCaptured[i] = mostRecentMeasure;
                break;
            }
            dataCaptured[i] = mostRecentMeasure;
            break;
        }

        return mostRecentMeasure;
    }
    else
    {
        throw new ArgumentOutOfRangeException(
            "Your units were neither of the available values.");
    }
}

关于C# 并非所有代码路径都返回值数据收集器类项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36897901/

相关文章:

c# - 使用委托(delegate)将方法签名传递给线程

c# - 如何更新 JSON 字符串中的属性值?

java - 该方法仅适用于类的单个实例,而不适用于 ArrayList

java - 调用Java方法

c++ - 从对象调用成员对象,错误: initial value of reference to non-const must be an lvalue

c# - 使用 DataTemplate 将 View 绑定(bind)到 ViewModel 时出现问题

c# - CS1525 : Invalid expression term '<'

javascript - 如何使用 return $.post - undefined 不是对象(评估 'function().then' )

javascript - 返回一个对象 - javaScript

scala - 何时在 Scala 中使用关键字 return