Author Topic: Select case, switch  (Read 8274 times)

0 Members and 1 Guest are viewing this topic.

Bryco

  • Water Moccasin
  • Posts: 1883
Select case, switch
« on: August 07, 2009, 02:16:33 PM »
I'm having a bit of trouble coming up with a good conversion from vba to c# with select cases.
In vba I can use a double dGauge
Code: [Select]
Select Case dGauge
        Case 0.035
            sGauge = " 20ga"
        Case 0.063
            sGauge = "x{\H0.7x;\S1#16;} WALL"
        Case 0.065
            sGauge = " 16ga"
        Case 0.083
            sGauge = " 14ga"
        Case 0.094
            sGauge = "x{\H0.7x;\S3#32;} WALL"
        Case 0.12
            sGauge = " 11ga"
        Case 0.125
            sGauge = "x{\H0.7x;\S1#8;} WALL"
        Case 0.188
            sGauge = "x{\H0.7x;\S3#16;} WALL"
            dGauge = 0.1875
        Case 0.25
            sGauge = "x{\H0.7x;\S1#4;} WALL"
        Case 0.313
            sGauge = "x{\H0.7x;\S5#16;} WALL"
            dGauge = 0.3125
        Case 0.375
            sGauge = "x{\H0.7x;\S3#8;} WALL"
         Case 0.5
            sGauge = "x{\H0.7x;\S1#2;} WALL"
    End Select
   

the same thing in C#
Code: [Select]
if(dGauge==0.035)
            {
                sGauge = " 20ga";goto skip;
            }
            if(dGauge== 0.063)
            {
                sGauge = @"x{\H0.7x;\S1#16;} WALL";goto skip;
            }
            if(dGauge== 0.065)
            {
                sGauge = " 16ga";goto skip;
            }
            if(dGauge== 0.083)
            {
                sGauge = " 14ga";goto skip;
            }
            if(dGauge== 0.094)
            {
                sGauge = @"x{\H0.7x;\S3#32;} WALL";goto skip;
            }
            if(dGauge== 0.12)
            {
                    sGauge = " 11ga";goto skip;
            }
            if(dGauge== 0.125)
            {
                sGauge = @"x{\H0.7x;\S1#8;} WALL";goto skip;
            }
            if(dGauge== 0.188)
            {
                sGauge = @"x{\H0.7x;\S3#16;} WALL";
                dGauge = 0.1875;goto skip;
            }
            if(dGauge==0.25)
            {
                sGauge = @"x{\H0.7x;\S1#4;} WALL";
            }
             if(dGauge== 0.313)
             {
                sGauge = @"x{\H0.7x;\S5#16;} WALL";
                dGauge = 0.3125;goto skip;
             }
             if(dGauge== 0.375)
             {
                sGauge = @"x{\H0.7x;\S3#8;} WALL";goto skip;
             }
             if(dGauge== 0.5)
             {
                sGauge = @"x{\H0.7x;\S1#2;} WALL";goto skip;
             }
         
        skip:

Is there a better way?

pkohut

  • Guest
Re: Select case, switch
« Reply #1 on: August 07, 2009, 02:23:22 PM »
Code: [Select]
if(blabla == lala)
{
  // do work
}
else if(blabla == haha)
{
  // do other work
}
else if(blabla == laha)
{
  // do different work
}
else
{
  // catch-all for anything not handled.
}

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Select case, switch
« Reply #2 on: August 07, 2009, 02:35:30 PM »
Thanks, for some reason that didn't work when I tried it before. Works now. Perhaps I used elseif

kpblc

  • Bull Frog
  • Posts: 396
Re: Select case, switch
« Reply #3 on: August 07, 2009, 02:37:56 PM »
Another way (I think) is:
Code: [Select]
switch dGauge
{
case 0.035 :
  sGauge = " 20ga";
  break;
case 0.063 :
  sGauge = "x{\H0.7x;\S1#16;} WALL";
  break;
// other
default :
  sGauge = "";
  break;
}
Sorry for my English.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Select case, switch
« Reply #4 on: August 07, 2009, 02:45:05 PM »
Does this work as expected?

Code: [Select]
    switch (dGauge)
    {
        case 0.035:
            sGauge = " 20ga";
            break;
        case 0.063:
            sGauge = @"x{\H0.7x;\S1#16;} WALL";
            break;
        case 0.065:
            sGauge = " 16ga";
            break;
        case 0.083:
            sGauge = " 14ga";
            break;
        case 0.094:
            sGauge = @"x{\H0.7x;\S3#32;} WALL";
            break;
        case 0.12:
            sGauge = " 11ga";
            break;
        case 0.125:
            sGauge = @"x{\H0.7x;\S1#8;} WALL";
            break;
        case 0.188:
            sGauge = @"x{\H0.7x;\S3#16;} WALL";
            dGauge = 0.1875;
            break;
        case 0.25:
            sGauge = @"x{\H0.7x;\S1#4;} WALL";
            break;
        case 0.313:
            sGauge = @"x{\H0.7x;\S5#16;} WALL";
            dGauge = 0.3125;
            break;
        case 0.375:
            sGauge = @"x{\H0.7x;\S3#8;} WALL";
            break;
        case 0.5:
            sGauge = @"x{\H0.7x;\S1#2;} WALL";
            break;
    }
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

pkohut

  • Guest
Re: Select case, switch
« Reply #5 on: August 07, 2009, 02:48:12 PM »
Doesn't C# switch/case states work with integers and strings only? 

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Select case, switch
« Reply #6 on: August 07, 2009, 02:50:53 PM »
not according to my compiler ... but then I could be wrong ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

pkohut

  • Guest
Re: Select case, switch
« Reply #7 on: August 07, 2009, 03:15:41 PM »
not according to my compiler ... but then I could be wrong ...

Ah heck you got me wondering - VS2008 tested -
Quote
C:\VS9_Projects\TestSwitchCase\TestSwitchCase\Program.cs(19,11): error CS0151: A value of an integral type expected
C:\VS9_Projects\TestSwitchCase\TestSwitchCase\Program.cs(23,10): error CS0152: The label 'error: non-I64 case' already occurs in this switch statement
C:\VS9_Projects\TestSwitchCase\TestSwitchCase\Program.cs(20,10): (Related location)
C:\VS9_Projects\TestSwitchCase\TestSwitchCase\Program.cs(26,10): error CS0152: The label 'error: non-I64 case' already occurs in this switch statement
C:\VS9_Projects\TestSwitchCase\TestSwitchCase\Program.cs(23,10): (Related location)
the switch statement

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Select case, switch
« Reply #8 on: August 07, 2009, 03:17:54 PM »
I just had a thought ... MSDN isn't perfectly clear on the switch/case issue with regards to using only strings and integers, but considering it won't compile for you, it wouldn't be impossible to resolve that by converting to a string prior to the switch/case ... and it would be a bit more efficient than the if/then/elseif/else .. I think.

Code: [Select]
switch (dGauge.ToString())
    {
        case "0.035":
            sGauge = " 20ga";
            break;
        case "0.063":
            sGauge = @"x{\H0.7x;\S1#16;} WALL";
            break;
        case "0.065":
            sGauge = " 16ga";
            break;
        case "0.083":
            sGauge = " 14ga";
            break;
        case "0.094":
            sGauge = @"x{\H0.7x;\S3#32;} WALL";
            break;
        case "0.12":
            sGauge = " 11ga";
            break;
        case "0.125":
            sGauge = @"x{\H0.7x;\S1#8;} WALL";
            break;
        case "0.188":
            sGauge = @"x{\H0.7x;\S3#16;} WALL";
            dGauge = 0.1875;
            break;
        case "0.25":
            sGauge = @"x{\H0.7x;\S1#4;} WALL";
            break;
        case "0.313":
            sGauge = @"x{\H0.7x;\S5#16;} WALL";
            dGauge = 0.3125;
            break;
        case "0.375":
            sGauge = @"x{\H0.7x;\S3#8;} WALL";
            break;
        case "0.5":
            sGauge = @"x{\H0.7x;\S1#2;} WALL";
            break;
    }
« Last Edit: August 07, 2009, 03:28:56 PM by Keith™ »
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Select case, switch
« Reply #9 on: August 07, 2009, 04:15:48 PM »
Keith I did think of using a string comparison, but in general switches are so much work to convert from vba I'm not sure they are worth  it.
I have many
Case a,b,c,d
which is now
case a:
case b:
case c:
case d:
yada yada;
break;

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Select case, switch
« Reply #10 on: August 07, 2009, 04:17:08 PM »
So you already have the code in VB?
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

pkohut

  • Guest
Re: Select case, switch
« Reply #11 on: August 07, 2009, 04:25:22 PM »
I just had a thought ... MSDN isn't perfectly clear on the switch/case issue with regards to using only strings and integers, but considering it won't compile for you, it wouldn't be impossible to resolve that by converting to a string prior to the switch/case ... and it would be a bit more efficient than the if/then/elseif/else .. I think.

Normally I'd make an argument for raw speed, but the differences between a "switch/case" statement and "if/else if" statement is so very small very that it's really a toss up as to which is used.  In terms of flexibility the "if" statement is better suited for some situations, as in the example given by the OP; but in his case casting the double to a string and comparing the value with "case" is probably fine.  However, doing so would/could never be more efficient than comparing the doubles in an "if/else if" statement. 

So just to counter and make a faster (not better cause of the magic numbers) "switch and or if/else if" version of the code I offer -
Code: [Select]
switch ((int) dGauge * 1000)
    {
        case 35: // "0.035"
            sGauge = " 20ga";
            break;
        case 63: // "0.063"
            sGauge = @"x{\H0.7x;\S1#16;} WALL";
            break;
        case 65: // "0.065"
            sGauge = " 16ga";
            break;
        case 83: // "0.083":
            sGauge = " 14ga";
            break;
        case 94: // "0.094":
            sGauge = @"x{\H0.7x;\S3#32;} WALL";
            break;
        case 120: // "0.12":
            sGauge = " 11ga";
            break;
        case 125: // "0.125":
            sGauge = @"x{\H0.7x;\S1#8;} WALL";
            break;
        case 188: // "0.188":
            sGauge = @"x{\H0.7x;\S3#16;} WALL";
            dGauge = 0.1875;
            break;
        case 250: // "0.25":
            sGauge = @"x{\H0.7x;\S1#4;} WALL";
            break;
        case 313: // "0.313":
            sGauge = @"x{\H0.7x;\S5#16;} WALL";
            dGauge = 0.3125;
            break;
        case 375: // "0.375":
            sGauge = @"x{\H0.7x;\S3#8;} WALL";
            break;
        case 500: // "0.5":
            sGauge = @"x{\H0.7x;\S1#2;} WALL";
            break;
    }


Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Select case, switch
« Reply #12 on: August 07, 2009, 04:28:33 PM »
I still can't figure out why I can compile it as a double and Bryco can't ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Glenn R

  • Guest
Re: Select case, switch
« Reply #13 on: August 07, 2009, 04:45:50 PM »
I would convert ToString() or create an enum type (this must be integral also)

Bryco

  • Water Moccasin
  • Posts: 1883
Re: Select case, switch
« Reply #14 on: August 07, 2009, 05:43:48 PM »
Keith, using C# 2008 express edition.
I am converting all my code to C#.
It's never ending.

The other bother I have had with switch is an error warning that the switch must be a constant.
I can't remember the exact context.

sinc

  • Guest
Re: Select case, switch
« Reply #15 on: August 07, 2009, 05:54:57 PM »
That means you tried to do something like this:

Code: [Select]
string a = "SomeValue";
string b = "SomeOtherValue";

switch (b)
{
  case a:
      // some code
      break;
}

That's illegal.  You can't use a variable in a "case" statement.  You must do something like this instead:

Code: [Select]
string b = "SomeOtherValue";

switch (b)
{
  case "SomeValue":
      // some code
      break;
}

Or I believe you can also do this:

Code: [Select]
const string a = "SomeValue";
string b = "SomeOtherValue";

switch (b)
{
  case a:
      // some code
      break;
}


It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: Select case, switch
« Reply #16 on: August 07, 2009, 06:14:14 PM »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Select case, switch
« Reply #17 on: August 07, 2009, 06:16:21 PM »
Keith
Are you using VB.NET or C#

.. 'cause the
VB.Net SELECT..CASE conditional testexpression must evaluate to one of the elementary data types (Boolean, Byte, Char, Date, Double, Decimal, Integer, Long, Object, Short, Single, and String).

whereas the

C# SWITCH..CASE conditional testexpression must evaluate to an integral or string type expression
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: Select case, switch
« Reply #18 on: August 07, 2009, 06:33:49 PM »
... but the differences between a "switch/case" statement and "if/else if" statement is so very small very that it's really a toss up as to which is used. ...


Paul is right, the compiler will create a jump-table out of the "if/else if" statements, so while it may not look as pretty as "switch/case", there is no performance issue.
I would still create the enum type, as you may be able to use it elsewhere in your code..


Bryco

  • Water Moccasin
  • Posts: 1883
Re: Select case, switch
« Reply #19 on: August 07, 2009, 07:24:36 PM »
Sinc, that's the one. Sure does limit the use.
Glenn and Daniel, the enum sounds good but is more work than I really want to do.
I'll still use switches as they are so readable but less often than in vba.
You've all put out some good ideas thanks.

pkohut

  • Guest
Re: Select case, switch
« Reply #20 on: August 07, 2009, 08:47:23 PM »
Sinc, that's the one. Sure does limit the use.
Glenn and Daniel, the enum sounds good but is more work than I really want to do.
I'll still use switches as they are so readable but less often than in vba.
You've all put out some good ideas thanks.

If you have a bunch of switch/case statements all operating on the same data type, you might
be able to get away with encapsulating the data and logic into a class, and adding utility member functions
as needed, especially if you're thinking of enum'ing the doubles somehow.

Upfront it could be a pain, but if it's doable the end result is very flexable, eliminates big
long "if and switch" statements, and acts as a fast reverse enum lookup.

Here's an example using the OP's data.  It builds a reverse lookup based type double to a GaugeInfo
object.  Expand utility as required.

(hmm, preview isn't showing indentation)
Code: [Select]
using System;
using System.Collections.Generic;

namespace TestSwitchCase
{
static class Gauges
{
public class GaugeInfo
{
public GaugeInfo(double dGauge, double dRealGauge, String sGauge)
{
// GaugeInfo Constructor, in this case 3 parameters.  If more are needed then also
// modify the GaugeInfo array as needed.
this.dGauge = dGauge; this.dRealGauge = dRealGauge; this.sGauge = sGauge;
}

public double Gauge() { return dGauge;  }
public double RealGauge() { return dRealGauge; }
public String GaugeString() { return sGauge; }

// Add any addition utility functions here.

// Sample utility function
public String GetWallThickness() { return RealGauge().ToString() + " wall Thickness"; }

private String sGauge;
private double dGauge;
private double dRealGauge;
}

// map a double value to GaugeInfo for fast lookups
static private Dictionary<double, GaugeInfo> lookup = new Dictionary<double, GaugeInfo>();

// constructors for the GaugeInfo array.
static private GaugeInfo[] gaugeInfo = {
new GaugeInfo(0.035, 0.035, " 20ga"),
new GaugeInfo(0.063, 0.063, @"x{\H0.7x;\S1#16;} WALL"),
new GaugeInfo(0.065, 0.065, " 16ga"),
new GaugeInfo(0.083, 0.083, " 14ga"),
new GaugeInfo(0.094, 0.094, @"x{\H0.7x;\S3#32;} WALL"),
new GaugeInfo(0.12, 0.12, " 11ga"),
new GaugeInfo(0.125, 0.125, @"x{\H0.7x;\S1#8;} WALL"),
new GaugeInfo(0.188, 0.1875, @"x{\H0.7x;\S3#16;} WALL"),
new GaugeInfo(0.25, 0.25, @"x{\H0.7x;\S1#4;} WALL"),
new GaugeInfo(0.313, 0.3125, @"x{\H0.7x;\S5#16;} WALL"),
new GaugeInfo(0.375, 0.0375, @"x{\H0.7x;\S3#8;} WALL"),
new GaugeInfo(0.5, 0.5, @"x{\H0.7x;\S1#2;} WALL"),

};

// Constructor, add all elements of GaugeInfo to the Dictionary
static Gauges()
{
foreach(GaugeInfo gi in gaugeInfo)
{
lookup.Add(gi.Gauge(), gi);
}
}

// Get GaugeInfo by index value.  Returns null if out of range
static public GaugeInfo get(int n)
{
if (n >= 0 && n < lookup.Count )
return gaugeInfo[n];
return null;
} // return gaugeInfo.ElementAtOrDefault(n); }

// return GaugeInfo by mapped double in Dictionary, returns null if not found
static public GaugeInfo get(double dGauge) {
GaugeInfo gaugeObject;
lookup.TryGetValue(dGauge, out gaugeObject);
return gaugeObject;
}
}

static class Program
{
[STAThread]
static void Main()
{
double dGauge = 0.0;
string sGauge = "";
string sWallThichness = "";
Gauges.GaugeInfo gi = Gauges.get(0.188);

if (gi != null)
{
dGauge = gi.RealGauge();
sGauge = gi.GaugeString();
sWallThichness = gi.GetWallThickness();

}

gi = Gauges.get(3);
if (gi != null)
{
dGauge = gi.RealGauge();
sGauge = gi.GaugeString();
sWallThichness = gi.GetWallThickness();

}

}
}
}


Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Select case, switch
« Reply #21 on: August 07, 2009, 09:01:53 PM »
Keith
Are you using VB.NET or C#

.. 'cause the
VB.Net SELECT..CASE conditional testexpression must evaluate to one of the elementary data types (Boolean, Byte, Char, Date, Double, Decimal, Integer, Long, Object, Short, Single, and String).

whereas the

C# SWITCH..CASE conditional testexpression must evaluate to an integral or string type expression

C# ... but I am still using the 2003 express version, and I have a hacked the configuration to allow a wider range of functionality not included in the express edition .. I know I should upgrade, but I have been lazy and besides I just use it for internal stuff anyway.

I read the MSDN at Microsoft and it has the same text as you provide, but the local installed stuff is strangely quiet.

Anyway, it works here, but not there .. that shouldn't be news to anyone who has used different versions of software though ...  :?  :?
considering the number of other responses, there are some good solutions here that resolve that problem though
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Spike Wilbury

  • Guest
Re: Select case, switch
« Reply #22 on: August 07, 2009, 09:34:33 PM »
Here's an example using the OP's data.  It builds a reverse lookup based type double to a GaugeInfo
object.


Dang.... you put it so easy and readable... some day when I get older will do things like that :)

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: Select case, switch
« Reply #23 on: August 07, 2009, 10:12:51 PM »
Ah come on Mr. Harrison, now I'm going to have that song stuck in my head all day  :laugh:

Spike Wilbury

  • Guest
Re: Select case, switch
« Reply #24 on: August 07, 2009, 11:46:39 PM »
Ah come on Mr. Harrison, now I'm going to have that song stuck in my head all day  :laugh:

This one: When i'm sixty-four ?

pkohut

  • Guest
Re: Select case, switch
« Reply #25 on: August 08, 2009, 01:29:31 AM »
Here's an example using the OP's data.  It builds a reverse lookup based type double to a GaugeInfo
object.


Dang.... you put it so easy and readable... some day when I get older will do things like that :)

Is that sarcasm  :wink: .  Actually, I was trying to shoehorn the OP's data into a command pattern.
Maybe it was a bit forced  :ugly:.  But I did say If you have a bunch of switch/case statements all
operating on the same data type
, you might be able to get away with encapsulating the data
and logic into a class.


Paul

pkohut

  • Guest
Re: Select case, switch
« Reply #26 on: August 08, 2009, 01:34:53 AM »
Ah come on Mr. Harrison, now I'm going to have that song stuck in my head all day  :laugh:

This one: When i'm sixty-four ?

I was thinking "Whip it!"

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8702
  • AKA Daniel
Re: Select case, switch
« Reply #27 on: August 08, 2009, 07:48:39 AM »
I was thinking "Whip it", But I knew you would think I meant "When I'm 64"   ;-)

Spike Wilbury

  • Guest
Re: Select case, switch
« Reply #28 on: August 08, 2009, 11:28:33 AM »
Nope is not sarcasm, actually it is an expression normally use in Spanish, when someone it is impressed, of the knowledge a person have or demonstrate and the way to say it indirectly, hope that makes sense now :).

ps> i knew you guys were thinking of whip it - whip it good!

Here's an example using the OP's data.  It builds a reverse lookup based type double to a GaugeInfo
object.


Dang.... you put it so easy and readable... some day when I get older will do things like that :)

Is that sarcasm   :wink: .  Actually, I was trying to shoehorn the OP's data into a command pattern.
Maybe it was a bit forced  :ugly:.  But I did say If you have a bunch of switch/case statements all
operating on the same data type
, you might be able to get away with encapsulating the data
and logic into a class.


Paul

« Last Edit: August 08, 2009, 11:33:53 AM by Esquivel »