存在我宣佈我的C#應用程序常數是這樣的:C#校驗值在不斷
public class Constant
public struct profession
{
public const string STUDENT = "Student";
public const string WORKING_PROFESSIONAL = "Working Professional";
public const string OTHERS = "Others";
}
public struct gender
{
public const string MALE = "M";
public const string FEMALE = "F";
}
}
我的驗證功能:
public static bool isWithinAllowedSelection(string strValueToCheck, object constantClass)
{
//convert object to struct
//loop thru all const in struct
//if strValueToCheck matches any of the value in struct, return true
//end of loop
//return false
}
在運行時,我會想在用戶輸入的值傳遞和該結構檢查結構中是否存在該值。結構可以是職業和性別。我怎樣才能實現它?
例子:
if(!isWithinAllowedSelection(strInput,Constant.profession)){
response.write("invalid profession");
}
if(!isWithinAllowedSelection(strInput,Constant.gender)){
response.write("invalid gender");
}
這聽起來像一個'enum'比結構更好的情況。 – RPM1984 2010-10-21 10:09:18