2010-11-25 78 views
1

我有這個代碼的兩個問題:爲什麼SWITCH ... CASE沒有使用'char'?

public int InsertOrUpdateRecord(char _code, string _databaseFileName) 
{ 

    switch(_code) 
    { 
     case 'N': 
      // Some code here 

     case 'U': 
      // Some code here 
    } 
     return 0; 
} 
  1. 它不接受焦炭單引號和雙引號值。
  2. 如果我通過_CODE爲,它提供了紅色下劃線在情況下與此錯誤:

Control無法通過一個case標籤下降到另一個。

+1

你只是缺少突破重點工作爲每個個案 - 見以利沙的代碼 – Berryl 2010-11-25 17:08:38

回答

12

的原因編譯錯誤是case缺少break

switch (_code) 
{ 
    case 'N': 
    // Some code here 
     break; // break that closes the case 

    case 'U': 
    // Some code here 
     break; // break that closes the case 
} 
+3

它** **不支持串! – 2010-11-25 17:09:55

2

您需要在情況到底做break

switch (_code) 
{ 
    case 'N': 
     // Some code here 
     Console.WriteLine("N was passed"); 
     break; 
    case 'U': 
     // Some code here 
     Console.WriteLine("U was passed"); 
     break; 
} 
-1

是沒有問題的與char。有關您的錯誤,請參見此msdn article

0

除非你想在任何情況下做同樣的,像這樣:

switch(_char) { 
    case 'N': 
    case 'U': 
     // Common code for cases N and U here... 
} 

你必須明確告訴編譯器在哪裏case語句停止:

switch(_char) { 
    case 'N': 
     // Code here... 
     break; // The N case ends here. 
    case 'U': 
     // Code here with different behaviour than N case... 
     break; // The U case ends here. 
} 

break語句告訴編譯器你完成了這種情況,並且它必須脫離switch指令。

0

您可以像下面的代碼一樣編寫break或return語句。

char _code ='U'; 開關(_CODE) { 情況下 'N':

   case 'U': 
        return; 
      } 

OR,

炭_CODE = 'U'; 開關(_CODE) { 情況下,「N」:

   case 'U': 
        break; 
      }