2016-05-09 73 views
-2

我們如何切換以下,如果-else報表切換case語句..任何人都可以幫助這個...開關elseif的切換情況下

 if (Webcc1.Contains(licensePartID)) 
     { 
      dtExpiryDate = dtActivatedDate.AddYears(1); 
      int isExpiry = DateTime.Compare(dtActivatedDate, dtExpiryDate); 
      if (isExpiry >= 0) 
      { 
        setError(lblSystemErr, "This action cannot be performed. The subscription period of the license key has expired"); 
        return; 
       } 
     } 
     else if (Webcc3.Contains(licensePartID)) 
     { 
      dtExpiryDate = dtActivatedDate.AddYears(3); 
      int isExpiry = DateTime.Compare(dtActivatedDate, dtExpiryDate); 
      if (isExpiry >= 0) 
      { 
        setError(lblSystemErr, "This action cannot be performed. The subscription period of the license key has expired"); 
        return; 
      } 
     } 
     else if (Webcc5.Contains(licensePartID)) 
     { 
      dtExpiryDate = dtActivatedDate.AddYears(5); 
      int isExpiry = DateTime.Compare(dtActivatedDate, dtExpiryDate); 
      if (isExpiry >= 0) 
      { 
       setError(lblSystemErr, "This action cannot be performed. The subscription period of the license key has expired"); 
       return; 
      } 
     } 

提前許多感謝....

+1

你不能。 Switch語句不能像那樣工作。如果它是'Webcc3 == licensePartID',你就有機會 – Rob

+0

有你的解決方案http://stackoverflow.com/questions/21255198/is-there-a-way-to-integrate-a-switch-with -string-包含 – elhampour

+0

@Rob:不可能只需要一段時間;-) –

回答

1

你不能直接這樣做,但你可以使用一個 位運算 三元條件轉換您如果...否則,如果到一個switch語句:

var flags = (Webcc1.Contains(licensePartID)) ? 1 : 0; 
flags |= (Webcc3.Contains(licensePartID)) ? 2 : 0; 
flags |= (Webcc5.Contains(licensePartID)) ? 4 : 0; 

var flags = (Webcc1.Contains(licensePartID)) ? 1 : 
      (Webcc3.Contains(licensePartID)) ? 2 : 
      (Webcc5.Contains(licensePartID)) ? 4 : 
      0; 
switch(flags) 
{ 
    case 1: 
     dtExpiryDate = dtActivatedDate.AddYears(1); 
     int isExpiry = DateTime.Compare(dtActivatedDate, dtExpiryDate); 
     if (isExpiry >= 0) 
     { 
      setError(lblSystemErr, "This action cannot be performed. The subscription period of the license key has expired"); 
      return; 
     } 
     break; 
    case 2: 
     dtExpiryDate = dtActivatedDate.AddYears(3); 
     int isExpiry = DateTime.Compare(dtActivatedDate, dtExpiryDate); 
     if (isExpiry >= 0) 
     { 
      setError(lblSystemErr, "This action cannot be performed. The subscription period of the license key has expired"); 
      return; 
     } 
     break; 
    case 4: 
     dtExpiryDate = dtActivatedDate.AddYears(5); 
     int isExpiry = DateTime.Compare(dtActivatedDate, dtExpiryDate); 
     if (isExpiry >= 0) 
     { 
      setError(lblSystemErr, "This action cannot be performed. The subscription period of the license key has expired"); 
      return; 
     } 
     break; 
} 
} 

然而,代碼示例您提供可以而且應該更短,因爲這是條件之間改變的唯一事情就是添加的年數,你可以簡單地這樣做:

var numberOfYears = (Webcc1.Contains(licensePartID)) ? 1 : 
        (Webcc3.Contains(licensePartID)) ? 3 : 
        (Webcc5.Contains(licensePartID)) ? 5 : 
        0; // or some other default number if needed 
dtExpiryDate = dtActivatedDate.AddYears(numberOfYears); 
int isExpiry = DateTime.Compare(dtActivatedDate, dtExpiryDate); 
if (isExpiry >= 0) 
{ 
    setError(lblSystemErr, "This action cannot be performed. The subscription period of the license key has expired"); 
    return; 
} 
+0

嗨Zohar很多謝謝....如果我不沒有任何默認編號結束..我需要檢查只有三個條件本身... –

+0

在這種情況下,只需檢查'numberOfYears'不是'0'在執行測試之前。 –