大家好我有一個實用程序類中的函數,它返回當前會話的用戶ID。 它拋出未設置爲對象實例的對象引用?我如何檢查它爲空&刪除此錯誤?在返回之前檢查會話嗎?
public static string GetSessionUserID
{
get
{
string userID = "";
if (System.Web.HttpContext.Current.Session["userID"].ToString() != null)
{
userID = System.Web.HttpContext.Current.Session["userID"].ToString();
}
if (userID == null)
{
throw new ApplicationException("UserID is null");
}
else
return userID;
}
}
感謝馬修。解釋真的很有幫助。你的意思是代替var嗎? – 2010-09-08 05:02:20
@Popo,它們應該是等價的(編譯器會用'object'替換'var')。不過,我同意在這裏「對象」更清晰。 – 2010-09-08 05:22:44
謝謝馬修。我prev。思想變量只存在於JavaScript中。 – 2010-09-08 07:17:45