Home All Groups Group Topic Archive Search About

switch vs Select Case

Author
11 Feb 2006 4:56 PM
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

Author
11 Feb 2006 4:59 PM
Carl Daniel [VC++ MVP]
ME wrote:
Show quote
> 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
Author
12 Feb 2006 1:51 AM
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 ;-)
Author
12 Feb 2006 6:30 AM
Cor Ligthert [MVP]
>> 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
Author
11 Feb 2006 5:43 PM
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

Show quote
"ME" <trash.trash@comcast.netREMOVETHIS> wrote in message
news:eqGdnSt-XsKwinPeRVn-sg@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
>
Author
11 Feb 2006 6:31 PM
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

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
12 Feb 2006 1:54 AM
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.
Author
12 Feb 2006 3:22 AM
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


Show quote
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:eWWpJmzLGHA.2580@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.
Author
12 Feb 2006 4:13 PM
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


Show quote
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in
message news:%23%232OdO4LGHA.3196@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" <mattias.dont.want.spam@mvps.org> wrote in message
> news:eWWpJmzLGHA.2580@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.
>
>
Author
12 Feb 2006 6:04 PM
Carl Daniel [VC++ MVP]
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
Author
13 Feb 2006 9:00 AM
Frans Bouma [C# MVP]
ME wrote:

Show quote
> 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


--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Author
14 Feb 2006 3:15 AM
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


Show quote
"ME" <trash.trash@comcast.netREMOVETHIS> wrote in message
news:ds2dnZzsJos-w3LenZ2dnUVZ_v6dnZ2d@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.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
|
|
| "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> wrote in
| message news:%23%232OdO4LGHA.3196@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" <mattias.dont.want.spam@mvps.org> wrote in message
| > news:eWWpJmzLGHA.2580@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.
| >
| >
|
|

AddThis Social Bookmark Button