473,378 Members | 1,544 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,378 software developers and data experts.

switch vs Select Case

ME
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;
case SimpleEnum.Two.ToString():
MessageBox.Show("Test 2");
break;
case SimpleEnum.Three.ToString():
MessageBox.Show("Test 3");
break;
}
}

The Visual Basic.NET version does not:

Public Sub Test(ByVal value As String)
Select Case value
Case SimpleEnum.One.ToString()
MessageBox.Show("Test 1")
Case SimpleEnum.Two.ToString()
MessageBox.Show("Test 2")
Case SimpleEnum.Three.ToString()
MessageBox.Show("Test 3")
Case Else
End Select
End Sub
How can this be done in C#?

Thanks,

Matt
Feb 11 '06 #1
11 3978
ME wrote:
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;
case SimpleEnum.Two.ToString():
MessageBox.Show("Test 2");
break;
case SimpleEnum.Three.ToString():
MessageBox.Show("Test 3");
break;
}
}

The Visual Basic.NET version does not:

Public Sub Test(ByVal value As String)
Select Case value
Case SimpleEnum.One.ToString()
MessageBox.Show("Test 1")
Case SimpleEnum.Two.ToString()
MessageBox.Show("Test 2")
Case SimpleEnum.Three.ToString()
MessageBox.Show("Test 3")
Case Else
End Select
End Sub
How can this be done in C#?


switch (value)
{
case "One":
// ...
}

AFIAK, that's the only way.

By the way, this newsgroup is for C++. You might try posting in the C#
newsgroup instead - microsoft.public.dotnet.languages.csharp

-cd
Feb 11 '06 #2
Not sure if this is what you are trying to do, but this works fine under 2.0
framework.

using System;

public class MyTestClass {

private enum Nums {
One = 1,
Two,
Three
}

public static void Test(string Value) {

switch(Value) {

case "One":
Console.WriteLine("Test One");
break;

case "Two":
Console.WriteLine("Test Two");
break;

case "Three":
Console.WriteLine("Test Three");
break;
}
}

public static int Main() {

Test(Enum.GetName(typeof(Nums), Nums.Two));
Test(Enum.GetName(typeof(Nums), Nums.One));
Test(Enum.GetName(typeof(Nums), Nums.Three));
Test(Enum.GetName(typeof(Nums), Nums.Two));

System.Console.Write("\nPress any key to continue...");
System.Console.ReadKey();

return 0;
}
}
Bye

"ME" <tr*********@comcast.netREMOVETHIS> wrote in message
news:eq********************@comcast.com...
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;
case SimpleEnum.Two.ToString():
MessageBox.Show("Test 2");
break;
case SimpleEnum.Three.ToString():
MessageBox.Show("Test 3");
break;
}
}

The Visual Basic.NET version does not:

Public Sub Test(ByVal value As String)
Select Case value
Case SimpleEnum.One.ToString()
MessageBox.Show("Test 1")
Case SimpleEnum.Two.ToString()
MessageBox.Show("Test 2")
Case SimpleEnum.Three.ToString()
MessageBox.Show("Test 3")
Case Else
End Select
End Sub
How can this be done in C#?

Thanks,

Matt

Feb 11 '06 #3
>How can this be done in C#?

Can't you switch on the enum type instead?

switch ((EimpleEnum)Enum.Parse(typeof(SimpleEnum), value))
{
case SimpleEnum.One:
MessageBox.Show("Test 1");
break;
case SimpleEnum.Two:
MessageBox.Show("Test 2");
break;
case SimpleEnum.Three:
MessageBox.Show("Test 3");
break;
}
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 11 '06 #4
> By the way, this newsgroup is for C++. You might try posting in the C#
newsgroup instead - microsoft.public.dotnet.languages.csharp

look better, it's not only the C newsgroup ;-)
Feb 12 '06 #5
> Can't you switch on the enum type instead?
That's the best answer!

anyway C# won't compile this:
switch(aString)
{
case anObj.ToString():
....
}
because, as the compiler says in its warning / error message, the case value
is not a constant.
Feb 12 '06 #6
Mattias,
I was going to recommend the same thing!

Switch on the Enum value itself...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:eW**************@TK2MSFTNGP14.phx.gbl...
| >How can this be done in C#?
|
| Can't you switch on the enum type instead?
|
| switch ((EimpleEnum)Enum.Parse(typeof(SimpleEnum), value))
| {
| case SimpleEnum.One:
| MessageBox.Show("Test 1");
| break;
| case SimpleEnum.Two:
| MessageBox.Show("Test 2");
| break;
| case SimpleEnum.Three:
| MessageBox.Show("Test 3");
| break;
| }
|
|
| Mattias
|
| --
| Mattias Sjögren [C# MVP] mattias @ mvps.org
| http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
| Please reply only to the newsgroup.
Feb 12 '06 #7
>> By the way, this newsgroup is for C++. You might try posting in the C#
newsgroup instead - microsoft.public.dotnet.languages.csharp

look better, it's not only the C newsgroup ;-)

Than still don't I understand why it is so wide handled in General and in
Framework.

The select case in VB is different from the one in C#, therefore I have as
well the idea that the C# newsgroup would be a much better place.

However we can do in th dotNet newsgroup of course as well XBox questions as
some want.

Just my idea

Cor
Feb 12 '06 #8
ME
I can see the enum example is throwing several off the point. Here is
another EXAMPLE of a time when I would like to use a dynamic switch:

private void WorkWithSelectedColumn(TemplateADataSet.SiteOption sRow row,
string columnName)
{

//Switch 1 works:
switch (columnName)
{
case "Key":
break;
case "Value":
break;
case "UniqueID":
break;
}

//Switch 2 does not work in c# but the vb version will work
switch (columnName)
{
case row.Key:
break;
case row.Value:
break;
case row.UniqueID:
break;
}
}

I realize that C# requires a constant. My question is, why give the VB guys
the "option" of creating a dynamic switch/select case but not give it to the
C# guys? Typically the way I handle this particular example is by using SQL
to generate some code constants (public constants of every column name of
every table in my data base) and use those in place of the "xxx" in switch
1. It would be nice to be able to perform the task in switch 2, which would
leave out a step (VS builds the dataset for me just fine).

Thanks,

Matt
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:%2******************@TK2MSFTNGP09.phx.gbl...
Mattias,
I was going to recommend the same thing!

Switch on the Enum value itself...

--
Hope this helps
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:eW**************@TK2MSFTNGP14.phx.gbl...
| >How can this be done in C#?
|
| Can't you switch on the enum type instead?
|
| switch ((EimpleEnum)Enum.Parse(typeof(SimpleEnum), value))
| {
| case SimpleEnum.One:
| MessageBox.Show("Test 1");
| break;
| case SimpleEnum.Two:
| MessageBox.Show("Test 2");
| break;
| case SimpleEnum.Three:
| MessageBox.Show("Test 3");
| break;
| }
|
|
| Mattias
|
| --
| Mattias Sjögren [C# MVP] mattias @ mvps.org
| http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
| Please reply only to the newsgroup.

Feb 12 '06 #9
ME wrote:
I realize that C# requires a constant. My question is, why give the
VB guys the "option" of creating a dynamic switch/select case but not
give it to the C# guys? Typically the way I handle this particular
example is by using SQL to generate some code constants (public
constants of every column name of every table in my data base) and
use those in place of the "xxx" in switch


Why? Because the VB language designers favored flexibility over efficiency
and the C# language designers went the other way. The equivalent (in every
way) code in C# is simply a cascade of if () {} else if () {}, ... The VB
compiler is just doing the work for you.

-cd
Feb 12 '06 #10
ME wrote:
I can see the enum example is throwing several off the point. Here
is another EXAMPLE of a time when I would like to use a dynamic
switch:

private void WorkWithSelectedColumn(TemplateADataSet.SiteOption sRow
row, string columnName)
{

//Switch 1 works:
switch (columnName)
{
case "Key":
break;
case "Value":
break;
case "UniqueID":
break;
}

//Switch 2 does not work in c# but the vb version will work
switch (columnName)
{
case row.Key:
break;
case row.Value:
break;
case row.UniqueID:
break;
}
}
These are 2 different types, so why should that work? You could try
parsing the columnname with System.Enum and compare it to one of the
enum values.
I realize that C# requires a constant. My question is, why give the
VB guys the "option" of creating a dynamic switch/select case but not
give it to the C# guys? Typically the way I handle this particular
example is by using SQL to generate some code constants (public
constants of every column name of every table in my data base) and
use those in place of the "xxx" in switch 1. It would be nice to be
able to perform the task in switch 2, which would leave out a step
(VS builds the dataset for me just fine).


It's syntactical sugar.
your first switch is compiled to a piece of code which stores all
strings in a static hashtable and which simply performs numeric tests.

FB
--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Feb 13 '06 #11
| //Switch 2 does not work in c# but the vb version will work
| switch (columnName)
| {
| case row.Key:
| break;
| case row.Value:
| break;
| case row.UniqueID:
| break;
| }

I would use a If/ElseIf/Else in both C# & VB.

Using VB's Select True in the above fashion never really felt right... One
of those "although you can do it, should you do it" features...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"ME" <tr*********@comcast.netREMOVETHIS> wrote in message
news:ds******************************@comcast.com. ..
|I can see the enum example is throwing several off the point. Here is
| another EXAMPLE of a time when I would like to use a dynamic switch:
|
| private void WorkWithSelectedColumn(TemplateADataSet.SiteOption sRow row,
| string columnName)
| {
|
| //Switch 1 works:
| switch (columnName)
| {
| case "Key":
| break;
| case "Value":
| break;
| case "UniqueID":
| break;
| }
|
| //Switch 2 does not work in c# but the vb version will work
| switch (columnName)
| {
| case row.Key:
| break;
| case row.Value:
| break;
| case row.UniqueID:
| break;
| }
| }
|
| I realize that C# requires a constant. My question is, why give the VB
guys
| the "option" of creating a dynamic switch/select case but not give it to
the
| C# guys? Typically the way I handle this particular example is by using
SQL
| to generate some code constants (public constants of every column name of
| every table in my data base) and use those in place of the "xxx" in switch
| 1. It would be nice to be able to perform the task in switch 2, which
would
| leave out a step (VS builds the dataset for me just fine).
|
| Thanks,
|
| Matt
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
| message news:%2******************@TK2MSFTNGP09.phx.gbl...
| > Mattias,
| > I was going to recommend the same thing!
| >
| > Switch on the Enum value itself...
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > "Mattias Sjögren" <ma********************@mvps.org> wrote in message
| > news:eW**************@TK2MSFTNGP14.phx.gbl...
| > | >How can this be done in C#?
| > |
| > | Can't you switch on the enum type instead?
| > |
| > | switch ((EimpleEnum)Enum.Parse(typeof(SimpleEnum), value))
| > | {
| > | case SimpleEnum.One:
| > | MessageBox.Show("Test 1");
| > | break;
| > | case SimpleEnum.Two:
| > | MessageBox.Show("Test 2");
| > | break;
| > | case SimpleEnum.Three:
| > | MessageBox.Show("Test 3");
| > | break;
| > | }
| > |
| > |
| > | Mattias
| > |
| > | --
| > | Mattias Sjögren [C# MVP] mattias @ mvps.org
| > | http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
| > | Please reply only to the newsgroup.
| >
| >
|
|
Feb 14 '06 #12

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

Similar topics

4
by: JaNE | last post by:
I'm trying to get some data from MySql database and to create switch based on those data, but as an php beginer, I have no clear idea how to do this. have tryed many options which loked promising,...
19
by: Robert Scheer | last post by:
Hi. In VBScript I can use a Select Case statement like that: Select Case X Case 1 to 10 'X is between 1 and 10 Case 11,14,16 'X is 11 or 14 or 16 End Select
6
by: Aristotelis E. Charalampakis | last post by:
Hi all, this is a newbie question :-) I was wondering if there was a way to use the switch statement in a manner that each case statement includes more that a simple value. i.e.: switch (...
11
by: Scott C. Reynolds | last post by:
In VB6 you could do a SELECT CASE that would evaluate each case for truth and execute those statements, such as: SELECT CASE True case x > y: dosomestuff() case x = 5: dosomestuff() case y >...
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. ...
3
by: John | last post by:
Is there a way in c# to have a switch statement with multiple expressions like in VB.net? Select Case e.Item.ItemType Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem...
16
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...
2
by: huzzaa | last post by:
I am trying to get the user to input 1-10 and that will select a a number from an array and place it into a variable. here is the code. do { cout << "Enter the first row...
2
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of...
5
by: richard | last post by:
I've been tossing around the idea now that maybe my problem might be better resolved by using the "select case"/switch command. The only problem is, all the dozens of sites I've seen so far only...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.