switch vs Select Case

M

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
 
C

Carl Daniel [VC++ MVP]

ME said:
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
 
B

Brooke

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
 
M

Mattias Sjögren

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
 
L

Lloyd Dupont

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 ;-)
 
L

Lloyd Dupont

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.
 
J

Jay B. Harlow [MVP - Outlook]

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


| >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.
 
C

Cor Ligthert [MVP]

By the way, this newsgroup is for C++. You might try posting in the C#
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
 
M

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.SiteOptionsRow 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
 
C

Carl Daniel [VC++ MVP]

ME said:
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
 
F

Frans Bouma [C# MVP]

ME said:
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.SiteOptionsRow
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


--
 
J

Jay B. Harlow [MVP - Outlook]

| //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


|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.SiteOptionsRow 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
|
|
| message | > 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
| >
| >
| > | > | >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.
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top