我得到一個錯誤,並非所有的代碼路徑都返回一個值?並非所有的代碼路徑都返回一個值
public string Authentication(string studentID, string password) // this line?
{
var result = students.FirstOrDefault(n => n.StudentID == studentID);
//find the StudentID that matches the string studentID
if (result != null)
//if result matches then do this
{
//----------------------------------------------------------------------------
byte[] passwordHash = Hash(password, result.Salt);
string HashedPassword = Convert.ToBase64String(passwordHash);
//----------------------------------------------------------------------------
// take the specific students salt and generate hash/salt for string password (same way student.Passowrd was created)
if (HashedPassword == result.Password)
//check if the HashedPassword (string password) matches the stored student.Password
{
return result.StudentID;
// if it does return the Students ID
}
}
else
//else return a message saying login failed
{
return "Login Failed";
}
}
由於您將魔法字符串'Login Failed'與學生ID混合在一起,因此您的設計非常難看。你可以使用'null'作爲失敗的返回值,或者將返回類型改爲更復雜的東西,或許是某種區別聯合。 – CodesInChaos 2012-04-24 09:42:04
你已經得到了你的問題的答案。不過,我建議你稍微重構這一點。不要將'Login Failed'作爲一個字符串返回,而應該返回'null',或者返回一個複雜的類型,表示登錄是否成功,以及學生的ID(或'null')。 – 2012-04-24 09:43:08
可能的重複[爲什麼我得到這個錯誤:不是所有的代碼路徑都返回一個值?](http://stackoverflow.com/questions/929608/why-am-i-getting-this-error-not-all-代碼路徑返回值) – 2012-05-01 18:36:19