473,809 Members | 2,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

switch case

In VB6 I could do the following

Select Case Right$(Text, 1)
Case "a","b"
do this
Case "c","d"
do this
Case else
do this
End Select
can I have multiple items per case in c#?

Thanks
Alan
Dec 19 '05 #1
3 36989
Alan,

Yes, but you would have to do it differently:

switch (Text.Substring (Text.Length - 1, 1))
{
case "a":
case "b":
// Do something

break;

case "c":
case "d":
// Do something else.

break;

default:

// Default case.
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Alan Roberts" <al**@statistix l.co.uk> wrote in message
news:uX******** ******@TK2MSFTN GP10.phx.gbl...
In VB6 I could do the following

Select Case Right$(Text, 1)
Case "a","b"
do this
Case "c","d"
do this
Case else
do this
End Select
can I have multiple items per case in c#?

Thanks
Alan

Dec 19 '05 #2
Thanks Nicholas. A bit more cumbersome but should do the trick!

Thanks again

Alan

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:u4******** ******@TK2MSFTN GP12.phx.gbl...
Alan,

Yes, but you would have to do it differently:

switch (Text.Substring (Text.Length - 1, 1))
{
case "a":
case "b":
// Do something

break;

case "c":
case "d":
// Do something else.

break;

default:

// Default case.
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Alan Roberts" <al**@statistix l.co.uk> wrote in message
news:uX******** ******@TK2MSFTN GP10.phx.gbl...
In VB6 I could do the following

Select Case Right$(Text, 1)
Case "a","b"
do this
Case "c","d"
do this
Case else
do this
End Select
can I have multiple items per case in c#?

Thanks
Alan


Dec 19 '05 #3
Alan Roberts <al**@statistix l.co.uk> wrote:
Thanks Nicholas. A bit more cumbersome but should do the trick!


Slightly less cumbersome would be to use the indexer instead of
Substring - this only works for a single character, however. (Note the
single quotes instead of double quotes):

switch (Text[Text.Length-1])
{
case 'a':
case 'b':
// Do something
break;
case 'c':
case 'd':
// Do something else.
break;
default:
// Default case.
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 20 '05 #4

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

Similar topics

35
8365
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 for '5'. So in his switch statement, he omits a case for '5':
10
9591
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the 'case' code snippet does not. (the code's function is illustrative.) ////////////////////////////////////////// //////// working 'if/else' switch //////// //////////////////////////////////////////
7
4063
by: Colin King | last post by:
Amusingly, one can use a while(0) statement to allow one to perform a switch statement without breaks. The while (0) enables the continue statements to break out of the switch. Ugly and beautiful! void sw(int s) { switch (s) while (0) { case 0: printf("zero\n");
3
19743
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. Now I find myself wanting to use "Select Case" i.e., "switch" in C# regularly, but I always have to find another way b/c C#'s switch statement only allows static or integral variables. For example, I often want to use a switch statement based on the...
10
12329
by: Evie | last post by:
I understand that when a switch statement is used without breaks, the code continues executing even after a matching case is found. Why, though, are subsequent cases not evaluated? I wrote a program to demonstrate how a switch without breaks behaves vs. how I expected it to behave. The code includes: (1) a switch statement with breaks (2) the if/else statements that have the same results as (1) (3) a switch statement without breaks...
11
3160
by: ME | last post by:
In C# the following code generates a compiler error ("A constant value is expected"): public void Test(string value) { switch (value) { case SimpleEnum.One.ToString(): MessageBox.Show("Test 1"); break;
6
4541
by: asit | last post by:
please modify to get the correct output.(switch case is compulsory) #include <stdio.h> int main() { char ch; printf("Enter any character : "); ch=getch(); switch(ch)
7
3364
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with parameters value and signal ID. Signal Id is used to get a user configurable parameter inside a configuration file, which depends on the type of switch. I have implemented it as under. Please ignore those magic numbers as I have mimized logic to...
11
4969
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Can switch statements be nested? I've got a large routine that runs off of a switch statement. If one of the switches in switch #1 is true, that enters switch statement #2. Some of the statements in switch #2 enter a 3rd switch. I don't receive any compile errors except whenever I attempt to add default switches to switches 2 or 3.
13
11864
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 just curious to find out is it effective to use this method?? or is there is any other alternative method present so that execution time and code size can be reduced?? Thanks in advance.
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10379
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7660
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.