【问题标题】:Not all code paths return a value in this function? [duplicate]并非所有代码路径都在此函数中返回值? [复制]
【发布时间】:2014-06-15 11:06:09
【问题描述】:

System.IO.File.GetLength 以字节为单位给出文件的大小。我正在尝试做的是采用该大小(以字节为单位)并将其转换为可能的最高单位,而不会让大小变得小于 1。例如,我想要在大小列中显示“2048”而不是“2 KB”显示。代码如下:

 private string sizeDown(string size)
    {
        decimal sizeoffile = Convert.ToDecimal(size);
        int downed = 0;

        do
        {
            sizeoffile = sizeoffile / 1024;
            downed += 1;
        }
        while (sizeoffile > 1024);

        if (downed > 3)
        {
            return "";
        }
        else
        {
            switch (downed)
            {
                case 0:
                    return Convert.ToString(sizeoffile) + " bytes";
                    break;
                case 1:
                    return Convert.ToString(sizeoffile) + " KB";
                    break;
                case 2:
                    return Convert.ToString(sizeoffile) + " MB";
                    break;
                case 3:
                    return Convert.ToString(sizeoffile) + " GB";



            }
        }
    }

但是,正如您可能从标题中看到的那样,Visual Studio 告诉我并非所有代码路径都返回值。我很困惑。什么大小的值会导致我的代码不返回值?

(顺便说一句,我知道我将大小作为字符串,然后将其转换为小数 - 我这样做是为了方便。)

【问题讨论】:

  • 如果downed 是负数怎么办?
  • 删除if(downed > 3) 并将其作为default 添加到switch 语句中。
  • else part deafult case 丢失
  • 您的代码也不适用于字节。删除do 关键字并将其替换为while。或者看我的例子。
  • 更好的副本:stackoverflow.com/q/2071345

标签: c#


【解决方案1】:

更改下面的行集

if (downed > 3)
        {
            return "";
        }
        else
        {
            switch (downed)
            {
                case 0:
                    return Convert.ToString(sizeoffile) + " bytes";
                    break;
                case 1:
                    return Convert.ToString(sizeoffile) + " KB";
                    break;
                case 2:
                    return Convert.ToString(sizeoffile) + " MB";
                    break;
                case 3:
                    return Convert.ToString(sizeoffile) + " GB";



            }
        }

到

        switch (downed)
        {
            case 0:
                return Convert.ToString(sizeoffile) + " bytes";
                break;
            case 1:
                return Convert.ToString(sizeoffile) + " KB";
                break;
            case 2:
                return Convert.ToString(sizeoffile) + " MB";
                break;
            case 3:
                return Convert.ToString(sizeoffile) + " GB";
            default : return ""; 

        }
    }

【讨论】:

  • 您还需要将break; 添加到default 案例中 - msdn.microsoft.com/en-us/library/06tc147t.aspx
  • 如果它是最后一条语句,你真的不需要 break .programmers.stackexchange.com/questions/201777/…
  • 是的,已解决,感谢您的帮助!
  • @KamleshArya,感谢您分享有关break的内部信息
  • 你真的不需要在你使用 return 语句的 switch 中的任何地方使用 break,因为 return 将有效地打破你的 switch,并且 break 永远不会真正被击中。
【解决方案2】:

将此代码与 Switch....Case 一起使用

 private string sizeDown(string size)
    {
        decimal sizeoffile = Convert.ToDecimal(size);
        int downed = 0;

        do
        {
            sizeoffile = sizeoffile / 1024;
            downed += 1;
        }
        while (sizeoffile > 1024);


            switch (downed)
            {
                case 0:
                    return Convert.ToString(sizeoffile) + " bytes";
                    break;
                case 1:
                    return Convert.ToString(sizeoffile) + " KB";
                    break;
                case 2:
                    return Convert.ToString(sizeoffile) + " MB";
                    break;
                case 3:
                    return Convert.ToString(sizeoffile) + " GB";
                    break;
                default:
                    return ""; 
                    break;

            }
        }

【讨论】:

  • 谢谢!它消除了错误。我实际上也在寻找类似“case else”的东西。一石两鸟,谢谢!
  • "case else",在default case 的switch... case 语句中已经注意
【解决方案3】:

这里错误的原因是编译器无法知道 downed 的值是什么,因为它将在运行时确定。即使您知道运行时值永远不会命中它,您也需要为每个值创建一个代码路径。正如其他回复中提到的,您可以在开关中添加默认返回值,或者如果这是您不希望遇到的情况,您可以抛出异常。

switch (downed)
        {
            case 0:
                return Convert.ToString(sizeoffile) + " bytes";
                break;
            case 1:
                return Convert.ToString(sizeoffile) + " KB";
                break;
            case 2:
                return Convert.ToString(sizeoffile) + " MB";
                break;
            case 3:
                return Convert.ToString(sizeoffile) + " GB";

            default:
                throw new InvalidOperationException();
        }

【讨论】:

    【解决方案4】:

    首先:当downed > 3 时,您的 switch-case 将不返回任何内容。这就是导致错误的原因。其次,您的解决方案将因字节而失败。删除 do 关键字并将其替换为 while 语句。

    要创建与其他答案完全不同的答案:

    private string sizeDown(string size)
    {
        long sizeOfFile = long.Parse(size);
        if (sizeOfFile == 1)
            return sizeOfFile + " byte"; 
        if (sizeOfFile < 1024)
            return sizeOfFile + " bytes"; 
        if (sizeOfFile < 1024 * 1024)
            return sizeOfFile / 1024 + " KB"; 
        if (sizeOfFile < 1024 * 1024 * 1024)
            return sizeOfFile / (1024 * 1024) + " MB"; 
        if (sizeOfFile < 1024 * 1024 * 1024 * 1024)
            return sizeOfFile / (1024 * 1024 * 1024) + " GB"; 
    
        return sizeOfFile / (1024 * 1024 * 1024 * 1024) + " TB"; 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 2013-10-06
      相关资源
      最近更新 更多