【问题标题】:What's wrong with this function. unable to return string value这个功能有什么问题。无法返回字符串值
【发布时间】:2012-05-01 21:23:05
【问题描述】:
public static string GetContentFromSPList(string cValueToFind)
{   
    string cValueFound = "";
    try
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite site = new SPSite("http://mysite"))
            {
                site.AllowUnsafeUpdates = true;
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;

                    SPList oListAbout = web.Lists["About"];
                    SPQuery oQuery = new SPQuery();

                    oQuery.Query = "<OrderBy><FieldRef Name='myField' /></OrderBy><Where><Eq><FieldRef Name='myField' /><Value Type='Choice'>" + cValueToFind + "</Value></Eq></Where>";

                    SPListItemCollection collListItems = oListAbout.GetItems(oQuery);

                    foreach (SPListItem oListItem in collListItems)
                    {
                        cValueFound = (oListItem["FieldContents"] != null ? oListItem["FieldContents"].ToString() : "");
                    }
                }
            }
            return cValueFound;
        });
        //return cValueFound;
    }
    catch (Exception ex)
    {
    }
    finally
    {
        //return cValueFound;
    }
}

上面是一段代码。

问题是不允许返回字符串。它不断给出编译错误。我确定我做错了什么!!。

谢谢。

【问题讨论】:

  • 您能发布您看到的错误消息吗?
  • 很明显,如果编译器给你一个错误,代码就是错误的。奇怪的是,产生的错误信息会告诉你到底是什么错误!
  • 我希望“并非所有代码路径都返回值”错误以...

标签: c# function return-value sharepoint-api elevated-privileges


【解决方案1】:

假设类似于:

“并非所有代码都返回值”。

如果是这样,只需添加

public static string GetContentFromSPList(string cValueToFind)
{   
       string cValueFound = "";
        try
        {
           //code
        }
        catch (Exception ex)
        {
        }
        finally
        {
           //some cleanup
        }

        return cValueFound ;
 }

【讨论】:

    【解决方案2】:

    将其放在方法的底部,因为如果捕获到异常,您不会返回。

        catch (Exception ex)
        {
            return cValueFound;
        }
        finally
        {
        }
    }
    

    【讨论】:

      【解决方案3】:

      您无法从finally返回,
      control cannot leave the body from finally clause 什么的)

      在 finally 之后或从 catch 移动返回

      【讨论】:

        【解决方案4】:

        只需在 finally 块下方添加您的 return 语句。

        不要在 try blck 中返回。

        【讨论】:

          【解决方案5】:

          我已经看到开发人员错过了很多次。发生这种情况的原因是因为一旦定义了函数的返回类型,那么函数应该在所有退出点都有一个 return 语句。在这种情况下,函数应该有一个返回语句,一个在 try 块的末尾,一个在 catch 块内,或者就像 Tigran 定义的那样,在底部的右边。如果您不打算从 catch 块中返回任何内容,则只需返回 null;

          public static string GetContentFromSPList(string cValueToFind)
          {   
                 string value= "";
                  try
                  {
                     //code
          return value;
                  }
                  catch (Exception ex)
                  {
          return null;
                  }
                  finally
                  {
                     //some cleanup
                  }
          
          
           }
          

          【讨论】:

            猜你喜欢
            • 2021-09-10
            • 2018-12-12
            • 2015-06-10
            • 1970-01-01
            • 2015-12-04
            • 1970-01-01
            相关资源
            最近更新 更多