-1

我写了 ac# 代码,这对我来说似乎是正确的

public static BCSMappedTable GetMappedTable(string p_ListName)
    {
        List<BCSDataBase> ConnexionList = BCSManagement.GetAllDataBases();
        bool found = false;
        foreach (BCSDataBase connexion in ConnexionList)
        {
            foreach (BCSMappedTable tabList in connexion.GetMappedTables())
            {
                if (tabList.getListeName().Equals(p_ListName))
                {
                    found = true;
                    return tabList;
                }
            }
        }
        if (found)
            return new BCSMappedTable();
    }

但是这个错误一直出现

error : not all code paths return a value

我不知道为什么!我精益我总是返回所需的值

4

6 回答 6

7

在函数结束时,如果found为 false,则您没有返回路径...

由于您return的循环中有一条语句,因此一旦找到该项目,该函数就会退出,因此您不需要该found变量。您可以使用以下内容:

public static BCSMappedTable GetMappedTable(string p_ListName)
{
    List<BCSDataBase> ConnexionList = BCSManagement.GetAllDataBases();
    foreach (BCSDataBase connexion in ConnexionList)
    {
        foreach (BCSMappedTable tabList in connexion.GetMappedTables())
        {
            if (tabList.getListeName().Equals(p_ListName))
            {
                // return as soon as the item is found
                return tabList;
            }
        }
    }

    // this code won't be executed if the item was found before...
    return new BCSMappedTable();
}
于 2012-09-26T11:17:53.877 回答
2

因为最后你有

if (found) 
            return new BCSMappedTable();

如果找不到怎么办?

问题是,它不会到达那里,因为当 found 设置为 true 时,你从函数返回 - 所以,同样,这一切需要说的是

return new BCSMappedTable();
于 2012-09-26T11:18:10.910 回答
1
public static BCSMappedTable GetMappedTable(string p_ListName)
    {
        List<BCSDataBase> ConnexionList = BCSManagement.GetAllDataBases();
        bool found = false;
        foreach (BCSDataBase connexion in ConnexionList)
        {
            foreach (BCSMappedTable tabList in connexion.GetMappedTables())
            {
                if (tabList.getListeName().Equals(p_ListName))
                {
                    found = true;
                    return tabList;
                }
            }
        }
        if (!found)
            return new BCSMappedTable();
    }
于 2012-09-26T11:21:08.713 回答
0

实现其他部分

if (found)
            return new BCSMappedTable();
else 
return tablist1;//or something else
于 2012-09-26T11:19:50.603 回答
0

在您的代码中,您有:

found = true;
return tabList;

给局部变量设置一个值然后直接返回是没有意义的,因为这个变量不会被使用。return基本上以您返回的值退出方法(它应该与您的方法的类型匹配)。

正如错误所述,并非所有路径都返回一个值。如果found==false没有返回值。

于 2012-09-26T11:20:56.180 回答
-2

试试这个

public static BCSMappedTable GetMappedTable(string p_ListName)
{
    List<BCSDataBase> ConnexionList = BCSManagement.GetAllDataBases();
    bool found = false;
    foreach (BCSDataBase connexion in ConnexionList)
    {
        foreach (BCSMappedTable tabList in connexion.GetMappedTables())
        {
            if (tabList.getListeName().Equals(p_ListName))
            {
                found = true;
                return tabList;
            }
        }
    }
    if (found)
      {
        return new BCSMappedTable();
       }
     return found;
}
于 2012-09-26T11:20:42.093 回答