2017-05-30 45 views
1

我在會議上由這樣的session中獲取&分配值存儲多個值空值:如何處理,同時從會話列表檢索值

var imageSessList = (List<string>)Session["ImagesNames"]; 

if (imageSessList != null) 
{ 
    string image1 = imageSessList[0]; 
    string image2 = imageSessList[1]; 
    string image3 = imageSessList[2]; 
    string image4 = imageSessList[3]; 
} 

但如果會話只包含3個值,所以在分配string image4 = imageSessList[3];時會拋出空錯誤。

如何在這種情況下處理null。

+2

首先檢查列表的長度。它也會拋出IndexOutOfRange異常,而不是null異常。試圖關閉作爲重複的https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f – Rob

回答

2

會話中的值可能爲空,因此您必須在投射它們之前檢查null。所以初始條件是if (Session["ImagesNames"] != null)現在可以安全地投射它們並分配給imageSessList。因此,變量imageSessList將包含會話變量中的項目,並且您需要根據其索引獲取這些項目,然後才能更好地檢查該數組索引的存在。因此,完整的代碼將如下所示:

if (Session["ImagesNames"] != null) 
{ 
    var imageSessList = (List<string>)Session["ImagesNames"]; 
    string image1 = imageSessList.Count>0? imageSessList[0]:""; 
    string image2 = imageSessList.Count>1? imageSessList[1]:""; 
    string image3 = imageSessList.Count>2? imageSessList[2]:""; 
    string image4 = imageSessList.Count>3? imageSessList[3]:""; 
    // Continue the job with these image variables 
    // Variables will be "" if those values are not found in the list 
} 
+0

它把變種imageSessList =(列表)後的工作會話[ 「ImagesNames」];在begininig – ace

+0

@ace:在會話中的值可能爲空,所以當你嘗試投'null'到'名單'導致錯誤,所以你應該執行鑄造 –

+0

1日之前檢查空我需要找回會話wethet null或不是由var imageSessList =(List )Session [「ImagesNames」]; 只有那麼我可以比較它是否爲空。 – ace

-1

請務必檢查該會話值第一的所有腦幹...

if(Session["ImagesNames"] != null){...} 

甚至更​​好的使用as操作...

var imageSessList = Session["ImagesNames"] as List<string>; 


      if (imageSessList != null) 
      { 
       string image1 = imageSessList[0]; 
       string image2 = imageSessList[1]; 
       string image3 = imageSessList[2]; 
       string image4 = imageSessList[3]; 
      } 

然後檢查所需的密鑰列表索引...確保您不會嘗試訪問索引中不存在的密鑰...

if(imageSessList.Count > 0) image1 = imageSessList[0]; 
if(imageSessList.Count > 1) image2 = imageSessList[1]; 
if(imageSessList.Count > 2) image3 = imageSessList[2]; 
0

你可以這樣做所有

var imageSessList = (List<string>)Session["ImagesNames"]; 


        if (imageSessList != null) 
        { 
         string img1= imageSessList.Count>0? imageSessList[0]:""; 
        } 
-1

爲什麼不在傳遞它的字符串變量之前檢查值是否爲空?

string image1 = imageSessList[3] ?? string.Empty; 
-2

試試這個。

if (imageSessList != null) 
       { 
        string image1 = Convert.Tostring(imageSessList[0]); 
        string image2 = Convert.Tostring(imageSessList[1]); 
        string image3 = Convert.Tostring(imageSessList[2]); 
        string image4 = Convert.Tostring(imageSessList[3]); 
       } 
+0

這將避免IndexOutOfRangeException? –

0

,如果你需要單獨處理每一個文件,你可以使用forforeach循環如下

var imageSessList = Session["ImagesNames"] as List<string>; 
foreach(string file in imageSessList) 
{ 
    //do something with file 
} 

,如果你需要4個文件要處理的東西,檢查的項目數,並繼續

if(imageSessList!=null && imageSessList.Count ==4) 
{ 
    string image1 = imageSessList[0]; 
    string image2 = imageSessList[1]; 
    string image3 = imageSessList[2]; 
    string image4 = imageSessList[3]; 
}else 
{ 
    // show error message 
} 
0

請檢查以下代碼行。通過這種方式,如果您的會話不包含第4項,第3項或第2項,則不會出現任何錯誤。如果項目在會話列表中存在,它將綁定項目值。

var imageSessList = (List<string>)Session["ImagesNames"]; 
if (imageSessList != null) 
{ 
    for (int i = 0; i < imageSessList.Count; i++) 
    { 
     if (imageSessList[i] != null && i == 0)      
     { 
      string image1 = imageSessList[i]; 
     } 
     if (imageSessList[i] != null && i == 1) 
     { 
      string image2 = imageSessList[i]; 
     } 
     if (imageSessList[i] != null && i == 2) 
     { 
      string image3 = imageSessList[i]; 
     } 
     if (imageSessList[i] != null && i == 3) 
     { 
      string image4 = imageSessList[i]; 
     } 
    } 
} 
+0

它就會給出一個錯誤,說此搜索不會在目前情況下存在,因爲我會在接下來的步驟,使用此搜索。這將無法訪問 – ace

+0

您可以在此聲明之前定義字符串變量,那麼你就可以訪問所有的人。 –