switch-statement

Select Case on an object's Type in VB.Net

I'm not sure if this valid C# but hopefully you get the idea. :) switch (msg.GetType()) { case ClassA: // blah case ClassB: // blah 2 case ClassC: // blah 3 } How would I switch on an object's type but using VB.NET's Select Case? I'm aware that some might suggest using polymorphism but I'm using a ...

Best way to do a PHP switch with multiple values ver case?

How would you do this PHP switch statement? Also note that these are much smaller versions, the 1 I need to create will have a lot more values added to it. Version 1: switch ($p) { case 'home': case '': $current_home = 'current'; break; case 'users.online': case 'users.location': case 'users.featu...

trouble making polymorphism defeat those switch/case statements

Continuing on previous questions (here, and here), I implemented a basic command pattern, created my command classes and coded to an interface, so when any command is used, to call the execute() method. However, I am still finding myself unable to shake those case statements: I am reading each character from a master/decision String, w...

PHP: Is it possible to use for loop inside a switch?

I'm attempting something like the following: switch ($p) { foreach ($modules as $m) { case '"'.$mod.'"': include 'modules/'.$m.'/cases.php'; break; } } but can't get it to work. Is it possible to use a for loop in this manner, inside a switch? ...

refactoring and removing case statements when circling over an enum structure

Hello, An enum structure declared in its own class is a member variable to the business logic class. That enum basically represents the state of that other class. Although I have revisited the issue several times, replacing, or getting rid of those case statements proves quite frustrating to me. Several business logic methods simple i...

tagging methods and calling them from a client object by tag

Hello, I have been trying to figure out a way to tag several methods from my base class, so that a client class can call them by tag. The example code is: public class Base { public void method1(){ ..change state of base class } public void method2(){ ..change state of base class } public void m...

Should I write one method to convert distances or a bunch of methods?

I'm just learning C++ and programming. I'm creating a class called Distance. I want to allow the user (programmer using), my class the ability to convert distances from one unit of measure to another. For example: inches -> centimeters, miles -> kilometers, etc... My problem is that I want to have one method called ConvertTo that will c...

Python: Does python have an equivalent to 'switch'?

I am trying to check each index in an 8 digit binary string. If it is '0' then it is 'OFF' otherwise its 'ON'. I'm wondering if there's a more concise way to write this code with a switch-like feature. Thanks, ...

How to convert an alphanumeric phone number to digits

UPDATE: The final version of my utility looks like this: StringBuilder b = new StringBuilder(); for(char c : inLetters.toLowerCase().toCharArray()) { switch(c) { case '0': b.append("0"); break; case '1': b.append("1"); break; case '2': case 'a': cas...

Recurrent if loop in a Switch

I would like to know if there is anyway I can do this without having to do the same If loop in each of the Switch cases since it's a bit repetitive. switch ($val) { case 1: if(!$object->doSomething1()==$goodValue) row .= $object->doSomething(); break; case 2: if(!$object->doSomething2()==$goodValue) row ...

Does "match ... true -> foo | false -> bar" have special meaning in Ocaml?

I encountered the following construct in various places throughout Ocaml project I'm reading the code of. match something with true -> foo | false -> bar At first glance, it works like usual if statement. At second glance, it.. works like usual if statement! At third glance, I decided to ask at SO. Does this construct have spec...

Using identity fields in a switch statement

Hi I have a SQL lookup table like this: CREATE TABLE Product(Id INT IDENTITY PRIMARY KEY, Name VARCHAR(255)) I've databound a ASP.NET DropDownList to a LLBLGen entity. User selects a product, and the Id get saved. Now I need to display some product specific details later on. Should I use the Product's ID, and hope the ID is always th...

What is the most terse syntax check for checking preconditions and then calling a switch statement?

What is the most terse syntax to combine a check for some preconditions with a switch statement? Can I combine an if/else and switch statement? if (!IsValid(text)) { DoSomeLogging(); } else { switch (text) { case "1": DoSomething(); break; case "2" DoSomethingElse(); break; default...

How can I use a switch() statement to convert from a numeric to a letter grade?

This IS NOT homework, but it is a practice that the teacher gave us to help study for a test. But i will not be turning this in to the teacher. (i'm hoping that is allowed) He wants a user to input a grade and have it assigned a letter grade. It would be easy with if statements, but i can't use ANY! I have to use the switch method. Her...

c ideas on shrinking this function?

I have this huge switch case with nested switch case statements in it that I was wondering if anyone had any ideas on how to clean up? switch (datatype) { case STR: { switch(operation) { case EQUALS: { /* perform str compare */ } case ...

How much statement can be made conditional in MySQL?

Hi, the MySQL manual says that a CASE statement (the WHEN part) can contain a statement_list. How much statement is that to be exact? I need to perform a pretty big query which I would like to parametrize. However this also implies that I need to change tables that are being joined. I've read that this cannot be done using CASE statemen...

Java Question: Is it possible to have a switch statement within another one?

I have a yes or no question & answer. I would like to ask another yes or no question if yes was the answer. My instructor wants us to use charAt(0) as input for the answer. Is it possible to have a switch statement within another one (like a nested if statement)? Thanks in advance. EDIT: Here is a sample of my pseudo code = displa...

jquery Using ranges in switch cases?

Switch cases are usually like Monday: Tuesday: Wednesday: etc. I would like to use ranges. from 1-12: from 13-19: from 20-21: from 22-30: Is it possible? I'm using javascript/jquery by the way. ...

What's the best(when performance matters) way to implement a state machine in C#?

I came up with the following options: Using the goto statement: Start: goto Data Data: goto Finish Finish: ; using the switch statement: switch(m_state) { case State.Start: m_state = State.Data; break; case State.Data: m_state = State.Finish; break; case State.Finish: break; } using goto and switch toge...

Mapping switch statements to data classes

This seems to come up alot in my code, i'm wondering if there is some way of removing the switch statement, or if there is a more elegant way of doing it? public class MetaData { public string AlbumArtist { get; set; } public string AlbumTitle { get; set; } public string Year { get; set; } public string SongTitle { g...