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

0 Members and 1 Guest are viewing this topic.

Bryco

  • Water Moccasin
  • Posts: 1882
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: 1882
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: 1882
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: 1882
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.