2016-05-16 8 views
-1

Ich habe versucht, diesen Code unten zu einer switch-Anweisung zu konvertieren, aber meine Fähigkeiten sind nicht perfekt in C#. Könnte mir bitte jemand mit diesem Fall helfen?Convert Wenn Anweisung zu wechseln

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

Braucht es eine Case-Anweisung zu sein? Sieht gut aus, wie es ist, ein Fall kann es ausführlicher machen, als es sein muss, da Sie die gleiche Aktion für viele der Optionen ausführen. –

Antwort

2

Sie können es so tun, wenn Sie C# 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; 
} 

Das ?.ToString() Dingen verwenden kehrt null wenn cookie["Location"]null ist. So behandelt diese beiden Bedingungen:

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

Und die anderen sind ziemlich einfach. Ich denke du kannst es verstehen.

IMO, der Schalter sieht nicht so gut aus wie das Original, wenn. Das If ist in diesem Fall wahrscheinlich leichter zu verstehen. Aber wenn Sie wirklich wie der Schalter, ist das auch in Ordnung.

-1

Wenn Sie auf jeden Fall eine Case-Anweisung wollen, dann sollte diese Arbeit:

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

Warum stimmen die Leute für eine Antwort ab, die funktioniert, ohne zu erklären, warum sie abgelehnt haben? – Rick

0

Ich glaube nicht, dass Sie Ihren Zustand ändern müssen wechseln, wenn Sie wirklich wollen Sie folgenden Code verwenden können.

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

Sie unter Code versuchen können:

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

dies versuchen,

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; 
     } 
Verwandte Themen