|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
switch vs Select Case("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 ME wrote:
Show quote > In C# the following code generates a compiler error switch (value)> ("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#? { 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 > By the way, this newsgroup is for C++. You might try posting in the C# look better, it's not only the C newsgroup ;-)> newsgroup instead - microsoft.public.dotnet.languages.csharp > >> By the way, this newsgroup is for C++. You might try posting in the C# Than still don't I understand why it is so wide handled in General and in >> newsgroup instead - microsoft.public.dotnet.languages.csharp >> > look better, it's not only the C newsgroup ;-) > 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 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 > >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. > 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. Mattias,
I was going to recommend the same thing! Switch on the Enum value itself... -- Show quoteHope 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. 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. > > ME wrote:
> I realize that C# requires a constant. My question is, why give the Why? Because the VB language designers favored flexibility over efficiency > 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 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 ME wrote:
Show quote > I can see the enum example is throwing several off the point. Here These are 2 different types, so why should that work? You could try> 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; > } > } 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 It's syntactical sugar.> 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). 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#) ------------------------------------------------------------------------ | //Switch 2 does not work in c# but the vb version will work I would use a If/ElseIf/Else in both C# & VB.| switch (columnName) | { | case row.Key: | break; | case row.Value: | break; | case row.UniqueID: | break; | } 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... -- Show quoteHope this helps Jay [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "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. | > | > | | |
|||||||||||||||||||||||