0
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; 
    } 
} 

上面是一段代码。这个功能有什么问题。无法返回字符串值

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

谢谢。

+1

你能发布你看到的错误消息吗? –

+0

显然,如果编译器给你一个错误,代码是错误的。奇怪的是,生成的错误信息告诉你到底是什么错误! – Tergiver

+0

我希望'不是所有的代码路径都返回一个值'错误,以......开始...... –

回答

2

它是这样的:

“不是所有的代码返回值”。

如果是这样,只需添加

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

     return cValueFound ; 
} 
1

将此放在方法的底部,因为如果发现异常,则不会返回。

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

不能从finally返回,
control cannot leave the body from finally clause或东西)

招回任后,终于还是从抓

0

只需添加你的回报下面的语句最后阻止。

不要在试图返回。

0

我见过开发人员错过了很多次。发生这种情况的原因是因为一旦你定义了函数的返回类型,那么函数应该在所有的出口点都有一个返回语句。在这种情况下,函数应该在try块的结尾处有一个返回语句,并且在Tigran定义的函数的底部有一个返回语句,或者一个在catch块的右侧。如果你不打算从catch块中返回任何东西,那么只返回null;

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


}