switch-statement

C# switch statement limitations - why?

When writing a switch statement there appears to be two limitations on what you can switch on and case statements. For example (and yes, I know, if you're doing this sort of thing it probably means your oo architecture is iffy - this is just a contrived example!):- Type t = typeof(int); switch (t) { case typeof(int): Con...

C# switch: case not falling through to other cases limitation

This question is kind of an add-on to this question In C#, a switch case cannot fall through to other cases, this causes a compilation error. In this case I am just adding some number to the month total for the selected month and each subsequent month thereafter. (simple example, not meant to be real) switch (month) { case 0: ...

Replacements for switch statement in python?

I want to write a function in python that returns different fixed values based on the value of an input index. In other languages I would use a switch or case statement, but python does not appear to have a switch statement. What are the recommended python solutions in this scenario? ...

Multiple Cases in Switch:

I believe I've seen this somewhere, but I don't recall if it was a different language, or if I just can't remember the syntax well. Is there a way to fall through multiple case statements without stating case value: repeatedly? I know this works: Switch (value) { case 1: case 2: case 3: //do some stuff break; ...

How do you switch on a string in XQuery?

I have an external variable coming in as a string and I would like to do a switch/case on it. How do I do that in xquery? ...

Switch over PropertyType

How can I make this work? switch(property.PropertyType){ case typeof(Boolean): //doStuff break; case typeof(String): //doOtherStuff break; default: break; } I don't want to use the name since string comparing for types is just awfull and can be subject to change. ...

Why can't variables be declared in a switch statement?

I've always wondered this - why can't you declare variables after a case label in a switch statement? In C++ you can declare variables pretty much anywhere (and declaring them close to first use is obviously a good thing) but the following still won't work: switch (val) { case VAL: // This won't work int newVal = 42; brea...

Objective-C switch using objects?

I'm doing some Objective-C programming that involves parsing an NSXmlDocument and populating an objects properties from the result. First version looked like this: if([elementName compare:@"companyName"] == 0) [character setCorporationName:currentElementText]; else if([elementName compare:@"corporationID"] == 0) [character setCo...

error when switching to different svn branch

I've got two SVN branches (eg development and stable) and want to switch from one to another... In every tutorial there is command like: rootOfLocalSvnCopy:>svn switch urlToNewBranch . But it leads in error in my case: svn: REPORT request failed on '/svn/rootOfLocalSvnCopy/!svn/vcc/default' svn: Cannot replace a directory from within ...

switch / pattern matching idea

I've been looking at F# recently, and while I'm not likely to leap the fence any time soon, it definitely highlights some areas where C# (or library support) could make life easier. In particular, I'm thinking about the pattern matching capability of F#, which allows a very rich syntax - much more expressive than the current switch/cond...

Eclipse for IntelliJ Idea Users

I have a coworker who is looking to switch from InteilliJ Idea to Eclipse, and is concerned about not knowing the Eclipse set of commands. I was wondering - would anyone have a link to keyboard mappings that can set Eclipse commands to at least sort of match Idea? Have you made this switch? Any "gotchas", tips, or info we should be a...

Switch statement fallthrough in C#?

Switch statement fallthrough is one of my personal major reasons for loving switch vs. if/else if constructs. An example is in order here: static string NumberToWords(int number) { string[] numbers = new string[] { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; string[] tens = n...

Tree Algorithm

I was thinking earlier today about an idea for a small game and stumbled upon how to implement it. The idea is that the player can make a series of moves that cause a little effect, but if done in a specific sequence would cause a greater effect. So far so good, this I know how to do. Obviously, I had to make it be more complicated (beca...

Switch Statement Fallthrough...should it be allowed?

For as long as I can remember I have avoided using switch statement fallthrough. Actually, I can't remember it ever entering my consciousness as a possible way to do things as it was drilled into my head early on that it was nothing more than a bug in the switch statement. However, today I ran across some code that uses it by design, whi...

Good way to do a "switch" in a Makefile

I'm experimenting with an updated build system at work; currently, I'm trying to find a good way to set compiler & flags depending on the target platform. What I would like to do is something like switch $(PLATFORM)_$(BUILD_TYPE) case "Linux_x86_release" CFLAGS = -O3 case "Linux_x86_debug" CFLAGS = -O0 -g case "ARM_rel...

SQL Switch/Case in where clause

I tried searching around but couldn't find anything that would help me out. I'm trying to do this in SQL: declare @locationType varchar(50); declare @locationID int; SELECT column1, column2 FROM viewWhatever WHERE CASE @locationType WHEN 'location' THEN account_location = @locationID WHEN 'area' THEN xxx_location_area = @locat...

How to use a switch case 'or' in PHP?

is there such thing (in PHP anyway) for an OR operator in a switch case? something like.. switch ($value) { case 1 || 2: echo 'the value is either 1 or 2'; break; } ...

Variable declaration in c# switch statement

Why is it that in a c# switch statement, for a variable used in multiple cases, you only declare it in the first case? For example, the following throws the error "A local variable named 'variable' is already defined in this scope". switch (Type) { case Type.A: string variable = "x"; break; case Type...

using the 'is' keyword in a switch in c#

I'm currently adding some new extended classes to this code: foreach (BaseType b in CollectionOfExtendedTypes) { if (b is ExtendedType1) { ((ExtendedType1) b).foo = this; } else if (b is ExtendedType2) { ((ExtenedType2) b).foo = this; } else { b.foo = this; } } and was curious if there is a way to use the i...

Can I have multiple cases that do the same thing?

The first one is definitely something that works, but which one below is the efficient way? switch($type) { case 1: print 'success'; break; case 2: print 'success'; break; case 3: print 'success'; break; case 4: print 'success for type 4'; break; } Since 1, 2 and 3 print do the sa...