我希望將會話的值轉換爲字符串,而無需在ASP.NET中進行類型轉換和轉換。如何在沒有類型轉換的情況下將會話值轉換爲字符串
但我想沒有任何轉換獲得Session [「用戶名」]的值到臨時變量。
示例 - 正確的方法是:
string temp;
Session["username"] = "Joy";
temp = Session["username"].ToString();
我希望將會話的值轉換爲字符串,而無需在ASP.NET中進行類型轉換和轉換。如何在沒有類型轉換的情況下將會話值轉換爲字符串
但我想沒有任何轉換獲得Session [「用戶名」]的值到臨時變量。
示例 - 正確的方法是:
string temp;
Session["username"] = "Joy";
temp = Session["username"].ToString();
一個會話持有的對象爲值,因此您需要將對象轉換爲正確的類型,以獲得該類型的強度。 會話值可以爲空,所以你需要檢查。
string temp;
Session["username"]="Joy";
temp=Session["username"] as string;
如果使用這樣的:
object temp = Session["username"]; // temp is just a basic object.
的另一個問題是,由於會話持有的對象,價值類型是「盒裝」爲對象。
你不能當你想案值類型使用「爲」劇組,所以:由於會議是Object類型的
int intTemp;
Session["num"]= 7; // int is boxed into an object
intTemp = (int)Session["num"]; // unboxed back to int
intTemp = Session["num"] as int; // cannot be done!!!
(所以你可以把任何東西),你需要將它轉換回字符串如果你想閱讀它。
這樣做的參訪方式是(因爲它可能不會在你的會話中尚)
string temp = Session["username"] as String;
if (temp == null)
{
//Doesn't exist in Session yet, so create and add
}
請試試這個..
temp=Session["username"] as string;
由於會議是一個對象,有必要鍵入貓它喜歡的類型..
*親愛的所有, 我知道會話是對象,但我想知道,我可以將會話的值轉換爲字符串witho任何類型的轉換或轉換。 它可能或不可以* –