switch-statement

Bitwise flags and Switch statement?

谁都会走 提交于 2019-12-21 09:29:05
问题 I have the following code (example), and I'm really not comfortable with so many 'if' checks: public enum Flags { p1 = 0x01, // 0001 p2 = 0x02, // 0010 p3 = 0x04, // 0100 p4 = 0x08 // 1000 }; public static void MyMethod (Flags flag) { if ((flag & Flags.p1) == Flags.p1) DoSomething(); if ((flag & Flags.p2) == Flags.p2) DosomethingElse(); if ((flag & Flags.p3) == Flags.p3) DosomethingElseAgain(); if ((flag & Flags.p4) == Flags.p4) DosomethingElseAgainAndAgain(); } MyMethod(Flags.p1 | Flags.p3);

Clojure case statement with classes

那年仲夏 提交于 2019-12-21 09:07:54
问题 I want to switch on the class of a given object in order to encode it. (defn encoded-msg-for [msg] (case (class msg) java.lang.Double (encode-double msg) java.lang.String (encode-str msg) java.lang.Long (encode-int msg) java.lang.Boolean (encode-bool msg) clojure.lang.PersistentArrayMap (encode-hash msg) clojure.lang.PersistentVector (encode-vec msg) nil "~" ) ) When I call (encoded-msg-for {}) , it returns No matching clause: class clojure.lang.PersistentArrayMap What is odd is that putting

Switching on UIButton title: Expression pattern of type 'String' cannot match values of type 'String?!'

狂风中的少年 提交于 2019-12-21 09:07:44
问题 I'm trying to use a switch in an @IBAction method, which is hooked to multiple buttons @IBAction func buttonClick(sender: AnyObject) { switch sender.currentTitle { case "Button1": print("Clicked Button1") case "Button2": print("Clicked Button2") default: break } When I try the above, I get the following error: Expression pattern of type 'String' cannot match values of type 'String?!' 回答1: currentTitle is an optional so you need to unwrap it. Also, the type of sender should be UIButton since

using collection of strings in a switch statement

梦想的初衷 提交于 2019-12-21 07:55:41
问题 I'm trying to find a solution for this problem. This is my example code: class Program { private string Command; private static string[] Commands = { "ComandOne", "CommandTwo", "CommandThree", "CommandFour" }; static void Main(string[] args) { Command = args[0]; switch(Command) { case Commands[0]: //do something break; case Commands[1]: //do something else break; case Commands[2]: //do something totally different break; case Commands[3]: //do something boring break; default: //do your default

C# 7 Pattern Match with a tuple

安稳与你 提交于 2019-12-21 07:41:41
问题 Is it possible to use tuples with pattern matching in switch statements using c# 7 like so: switch (parameter) { case ((object, object)) tObj when tObj.Item1 == "ABC": break; } I get an error that says tObj does not exist in the current context . I have tried this as well: switch (parameter) { case (object, object) tObj when tObj.Item1 == "ABC": break; } This works fine: switch (parameter) { case MachineModel model when model.Id == "123": break; } 回答1: Remember that C#7 tuples are just

C# 7 Pattern Match with a tuple

痴心易碎 提交于 2019-12-21 07:41:15
问题 Is it possible to use tuples with pattern matching in switch statements using c# 7 like so: switch (parameter) { case ((object, object)) tObj when tObj.Item1 == "ABC": break; } I get an error that says tObj does not exist in the current context . I have tried this as well: switch (parameter) { case (object, object) tObj when tObj.Item1 == "ABC": break; } This works fine: switch (parameter) { case MachineModel model when model.Id == "123": break; } 回答1: Remember that C#7 tuples are just

switch statement in C / Objective C

瘦欲@ 提交于 2019-12-21 07:18:10
问题 I am new to Objective-C, But from all I know about switch statements, the following code should not print anything, because to me it seems that there should be a compilation error. However 2 is printed. I do not understand why. Could someone please explain? - (void) test { int myVar = 2; switch (myVar) { case 1: { NSLog(@"result: %d", myVar); break; case 2: { NSLog(@"result: %d", myVar); } break; } } } 回答1: You can think of the switch(value){case label:, ...} construct as a variable goto

Is there a way to make my switch/case fall through to the next case in C#?

别来无恙 提交于 2019-12-21 07:12:09
问题 I'm using a switch/case statement to handle some updates for a deployed application. Basically, I want to waterfall through the cases to perform the update from the current running version to the newest version. From Visual Studio yelling at me, I learned that C# does not allow falling through to the next case (exactly what I'm trying to do). From this question, I learned how to do what I want to do. However, it is still apparently an error. What I've got is switch (myCurrentVersion) { case

switch-case statement without break

让人想犯罪 __ 提交于 2019-12-21 07:12:03
问题 According to this book I am reading: Q What happens if I omit a break in a switch-case statement? A The break statement enables program execution to exit the switch construct. Without it, execution continues evaluating the following case statements. Suppose if I have codes looking like switch (option}{ case 1: do A; case 2: do B; default: do C; break; } Does this mean if I choose case 1, the A and C are done. If I choose case 2, B and C are done. If i choose neither, then only C is done. if

How to write Switch Statement in angularJS Controller

为君一笑 提交于 2019-12-21 07:08:35
问题 How to write Switch Statement in angularJS Controller ? My Source Code is <!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <body> <div ng-app="myApp" ng-controller="customersCtrl"> <table> <tr ng-repeat="x in names"> <td>{{ x.Name }}</td> <td><a href="" ng-click="SwitchFuction(x.Name, x.Sno)">{{ x.Country }}</a></td> </tr> </table> </div> <script> var app = angular.module('myApp', []); app.controller('customersCtrl', function(