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

0 Members and 1 Guest are viewing this topic.

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: 8691
  • 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: 8691
  • 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: 8691
  • 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: 8691
  • 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 »