2012-11-17 183 views
0

過去幾天我一直在試圖解決這個問題,但沒有成功。我基本上有4數據庫字段可以存儲用戶的圖片,因此用戶可以有多達4張圖片。字段名稱是picture1,picture2,picture3和picture4。 我正在嘗試做什麼是當用戶上傳圖片時,檢查picture1是否爲NULL,如果不是則保存圖片的文件名,然後下一張圖片轉到圖片2,如果不是NULL,則保存在那裏等等直到picture4。IF語句轉換成SWITCH語句

什麼是有是這樣的:

if (openhouse.picture1 == null) 
       { 
        openhouse.picture1 = changename; 

       } 
      // if picture2 is NULL and picture1 is not NULL then insert picture 
if (openhouse.picture2 == null && openhouse.picture1 != null) 
       { 
        openhouse.picture2 = changename; 

       } 
      // if picture3 is NULL and picture2 is not NULL then insert picture 
if (openhouse.picture3 == null && openhouse.picture2 != null) 

       { 
        openhouse.picture3 = changename; 

       } 
       // if picture4 is NULL and picture3 is not NULL then insert picture 
    if (openhouse.picture4 == null && openhouse.picture3 != null) 
       { 
        openhouse.picture4 = changename; 

       } 

正如你可以看到我的問題是,只要你上傳自相同的圖片被上傳到所有領域的圖片IF語句有沒有打破我的問題是:是否有某種方式,我可以將此if語句轉換爲switch語句,只要條件成立就使用BREAKS,它會停止評估其餘部分。

回答

2

不需要switch語句 - 只需使用else if

if (condition #1) 
{ 
    // block #1 
} 
else if (condition #2) 
{ 
    // block #2 
} 

如果第一個條件爲真,則塊#2不會被執行。

if (openhouse.picture1 == null) 
    openhouse.picture1 = changename; 
else if (openhouse.picture2 == null) 
    openhouse.picture2 = changename; 
// etc. 
+0

謝謝你終於工作了..感謝它! – user1591668