473,324 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

switch statement in c#

Hi,
I'm trying to do a very easy thing in SWITCH statment in C# but for some
reasons I can't do it (I'm getting errors messages,probably I'm missing
something)

What I want to do is this :

int n = aObject.FunctionOfIntergerType;

switch (n)
{
case 1,2 : dothis; // <- how can I group multiple values in one case
?????
case 3 :dotThat;
}

Thank You.
Nov 16 '05 #1
4 29212
Hi,

You can't have multiple case statements like that in C#.
However, in simple cases like that you can allow a fallthrough to the next statement.

switch(n)
{
case 1:
case 2:
dothis;
break;
case 3:
dothat;
break;
}

A fallthrough is only allowed if the case is empty.

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Hi Genc,

You can do a fall through like what Morten recommended or you can use goto
statement in you swicth statements :)

case 1:
goto case 3;
case 2:
Console.WriteLine("OOps");
break;
case 3:
Console.WriteLine("Nah");
break;
default:
Console.WriteLine("Wrong");
break;

Have fun!

"genc ymeri" wrote:
Hi,
I'm trying to do a very easy thing in SWITCH statment in C# but for some
reasons I can't do it (I'm getting errors messages,probably I'm missing
something)

What I want to do is this :

int n = aObject.FunctionOfIntergerType;

switch (n)
{
case 1,2 : dothis; // <- how can I group multiple values in one case
?????
case 3 :dotThat;
}

Thank You.

Nov 16 '05 #3
thanks a lot :)

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opsexnjavjklbvpo@stone...
Hi,

You can't have multiple case statements like that in C#.
However, in simple cases like that you can allow a fallthrough to the next statement.
switch(n)
{
case 1:
case 2:
dothis;
break;
case 3:
dothat;
break;
}

A fallthrough is only allowed if the case is empty.

--
Happy coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #4
thnx :)

"Chua Wen Ching" <ch************@nospam.hotmail.com> wrote in message
news:4C**********************************@microsof t.com...
Hi Genc,

You can do a fall through like what Morten recommended or you can use goto
statement in you swicth statements :)

case 1:
goto case 3;
case 2:
Console.WriteLine("OOps");
break;
case 3:
Console.WriteLine("Nah");
break;
default:
Console.WriteLine("Wrong");
break;

Have fun!

"genc ymeri" wrote:
Hi,
I'm trying to do a very easy thing in SWITCH statment in C# but for some
reasons I can't do it (I'm getting errors messages,probably I'm missing
something)

What I want to do is this :

int n = aObject.FunctionOfIntergerType;

switch (n)
{
case 1,2 : dothis; // <- how can I group multiple values in one case
?????
case 3 :dotThat;
}

Thank You.

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
17
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can...
5
by: Alvin Bruney | last post by:
Is a switch more efficient than an if statement? I observe thru the debugger that a switch statement jumps directly to its case handler where as an if statement examines all conditions...
3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
27
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
25
by: v4vijayakumar | last post by:
'continue' within switch actually associated with the outer 'while' loop. Is this behavior protable? int ch = '\n'; while (true) { switch(ch) { case '\n': cout << "test"; continue; } }
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
2
by: Phillip B Oldham | last post by:
What would be the optimal/pythonic way to subject an object to a number of tests (based on the object's attributes) and redirect program flow? Say I had the following: pets = {'name':...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.