我仍然得到錯誤,即使我已經有一個default
條款並非所有的路徑返回開關值
bool Validate(TextBox textBox , string type)
// textboxL - textbox accept only letters of alphabets and space.Is Mandatory
// textboxS - textbox accept letters of alphabets , space , - , _ .Is Mandatory
// return true by default
{
switch(type)
{
case "textboxL":
if (!Regex.IsMatch(textBox.Text, @"^[a-zA-Z]+$") || String.IsNullOrEmpty(textBox.Text)) {
MessageBox.Show("Invalid!!" + textBox.Text + "must contain only letters and shouldn't be empty");
return false;
}
break;
case "textboxS":
if (!Regex.IsMatch(textBox.Text, @"^[a-zA-Z-_ ]+$") || String.IsNullOrEmpty(textBox.Text)) {
return false;
}
break;
default: return true;
}
}
您的返回語句在if語句塊中,因此您的路徑不會以return語句結束。 – juharr
由於您已經在切換案例中使用if條件,爲什麼不簡單地創建完整的if-結構?這樣,你就能夠很快找到你的bug。 –