[英]How do I solve this error of 'not all code paths return a value'?
function 的名稱是產生錯誤的地方。 它指出“並非所有代碼路徑都返回一個值”。 為什么我會收到此錯誤,我該如何糾正?
public List<List<String>> getAvailablePMs()
{
public List<List<String>> allAvailablePMs = new List<List<String>>;
for (int s = 0; s < MyStaticValues.PMmanagerArray.Count; s++)
{ //for each PM in PMmanagerArray (excluding faulty or in-use)
if (MyStaticValues.PMmanagerArray[s][1] == ""
&& MyStaticValues.PMmanagerArray[s][2].Equals(0)
&& MyStaticValues.PMmanagerArray[s][3].Equals(0))
{//if the PM assigned bay is NULL AND PM status is not in use AND not faulty
allAvailablePMs.push(MyStaticValues.PMmanagerArray[s][0]);
}
}
return allAvailablePMs;
}
您不能在方法中使用public
,這會導致編譯錯誤,使編譯器感到困惑,使其抱怨“並非所有代碼路徑都返回值”。
此外,您的代碼還有其他錯誤, List
中沒有push
方法,您應該使用Add
。 此外,如果MyStaticValues.PMmanagerArray[s][0]
是string
,則不能將其添加到allAvailablePMs
,因為它需要List<string>
。
在風格上,C# 中的所有方法和屬性都是 PascalCase,而不是 camelCase,這是一個約定,因此您的方法名稱應該是GetAvailablePMs
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.