2016-05-16 10 views
-1

このコードをswitch文に変換しようとしましたが、私のスキルはC#では完璧ではありません。この場合、誰かが私を助けてくれますか?Convert If switch to switch

protected void logobtn_Click(object sender, ImageClickEventArgs e) 
{ 
    HttpCookie cookie = Request.Cookies.Get("Location"); 

    if (cookie["Location"] == null || 
     cookie["Location"].ToString() == null || 
     cookie["Location"].ToString() == "" || 
     cookie["Location"].ToString() == "-- Europe and Eastern Europe --" || 
     cookie["Location"].ToString() == "-- Asia & South-East Asia --" || 
     cookie["Location"].ToString() == "-- North & South of America --" 
    ) { 
     Response.Redirect("Index.aspx"); 
    } else { 
     Response.Redirect(string.Format("Berava.aspx?Country={0}", cookie.Value)); 
    } 
} 
+1

それはcase文である必要はない、これを試してみてください?あなたは多くのオプションで同じアクションを行っているので、ケースはもっと冗長にする必要があるかもしれません。 –

答えて

2

あなたはこのようにそれを行うことができますが6

switch (cookie["Location"]?.ToString()) { 
    case null: 
    case "": 
    case "-- Europe and Eastern Europe --": 
    case "-- Asia & South-East Asia --": 
    case "-- North & South of America --": 
     Response.Redirect("Index.aspx"); 
     break; 
    default: 
     Response.Redirect(string.Format("Berava.aspx?Country={0}", cookie.Value)); 
     break; 
} 

?.ToString()ブツはnullを返します。したがって、これらの2つの条件を処理します:

cookie["Location"] == null || 
cookie["Location"].ToString() == null 

その他はかなり簡単です。あなたが理解できると思います。

IMO、スイッチは元のようには見えません。この場合、ifはおそらく分かりやすいでしょう。しかし、スイッチのようなが本当になら、それはいいですね。

-1

あなたは間違いなく、これは動作するはずcase文をしたい場合:

HttpCookie cookie = Request.Cookies.Get("Location"); 

if (cookie != null) 
{ 
    string cookieString = cookie["Location"].ToString(); 

    switch (cookieString) 
    { 
     case "": 
     case "-- Europe and Eastern Europe --": 
     case "-- Asia & South-East Asia --": 
     case "-- North & South of America --": 
      Response.Redirect("Index.aspx"); 
      break; 
     default: 
      Response.Redirect(string.Format("Berava.aspx?Country={0}", cookie.Value)); 
      break; 
    } 
} 
else 
{ 
    //this is the null case... 
    Response.Redirect("Index.aspx"); 
} 
+0

なぜ投票した理由を説明せずに働く回答に対して投票を行うのはなぜですか? – Rick

0

私はあなたが本当にあなたは以下のコードを使用することができますしたい場合は、切り替えるようにあなたの状態を変更する必要はないと思います。あなたは、コードの下にしようとするかもしれ

string coo = Convert.ToString(Request.Cookies["Location"]==null?null:Request.Cookies["Location"].Value); 
       switch(coo) 
       { 
        case null: 
         Response.Redirect("Index.aspx"); 
        case "": 
         Response.Redirect("Index.aspx"); 
        case "-- North & South of America --": 
         Response.Redirect("Index.aspx"); 
        case "-- Asia & South-East Asia --": 
         Response.Redirect("Index.aspx"); 
        case "-- Europe and Eastern Europe --": 
         Response.Redirect("Index.aspx"); 
        default: 
         Response.Redirect(string.Format("Berava.aspx?Country={0}", coo)); 
} 
1

cookie["Location"]nullあるときは、C#を使用している場合

HttpCookie cookie = Request.Cookies.Get("Location"); 

      if (cookie["Location"]==null) 
       Response.Redirect(string.Format("Berava.aspx?Country={0}", cookie.Value)); 
else 
      switch (cookie["Location"].ToString()) 
      { 
       case null: 
       case "": 
       case "-- Europe and Eastern Europe --": 
       case "-- Asia & South-East Asia --": 
       case "-- North & South of America --":  
        Response.Redirect("Index.aspx"); 
        break; 
       default: 
        Response.Redirect(string.Format("Berava.aspx?Country={0}", cookie.Value)); 
        break; 
      } 
1

HttpCookie cookie = Request.Cookies.Get("Location"); 

     switch (cookie["Location"]) 
     { 
      case null : 
      case "": 
      case "--Europe and Eastern Europe--" : 
      case "-- Asia & South-East Asia --": 
      case "-- North & South of America --": 
       Response.Redirect("Index.aspx"); 
       break; 
      default: 
       Response.Redirect(string.Format("Berava.aspx?Country={0}", cookie.Value)); 
       break; 
     } 
0
 var cookie = Request.Cookies.Get("Location")?["Location"]; 

     switch (cookie) 
     { 
      case null: 
       Response.Redirect("Index.aspx"); 
       // DO STUFF 
       break; 
      case "": 
       // DO STUFF 
       break; 
      case "-- Europe and Eastern Europe --": 
       // DO STUFF 
       break; 
      case "-- Asia & South-East Asia --": 
       // DO STUFF 
       break; 
      case "-- North & South of America --": 
       // DO STUFF 
       break; 
      default: 
       Response.Redirect(string.Format("Berava.aspx?Country={0}", cookie.Value)); 
       break; 
     }