switch statement

  • Thread starter Thread starter Just Me
  • Start date Start date
J

Just Me

I know you can stack case(s)

case 1:
case 2:


but can you have ranges as in vb.net does like ( 1 to 6 ) type of thing, or
<n or > n etc.
 
Just said:
I know you can stack case(s)

case 1:
case 2:


but can you have ranges as in vb.net does like ( 1 to 6 ) type of
thing, or <n or > n etc.

No, you can't. So in those situations, you either have to stack cases
on top of eachother or use a christmastree of if/else statements :)

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
Just Me said:
I know you can stack case(s)

case 1:
case 2:


but can you have ranges as in vb.net does like ( 1 to 6 ) type of thing,
or <n or > n etc.

No, not in C#. You will have to stack some "if... else if..." blocks to
do that.
 
I know you can stack case(s)

case 1:
case 2:


but can you have ranges as in vb.net does like ( 1 to 6 ) type of thing, or
<n or > n etc.

No. Translate those into if/else blocks.
 
Thanks Guys.

Maybe its just me, but this seems a bit limiting really. vb.net select/case
case(s) are more flexible than c# in this particular case ( no pun
intended ).

:-D

Cheers
 
Maybe its just me, but this seems a bit limiting really. vb.net select/case
case(s) are more flexible than c# in this particular case ( no pun
intended ).

They're just different ways of expressing the same thing. Depending on
the ranges involved, the VB compiler may choose to compile it as an
if/else anyway.

I agree that the VB.NET version is more flexible, but I wouldn't say
that C# actually limits you - it doesn't stop you doing anything, it
just makes you express it in a different way.
 
Just said:
Maybe its just me, but this seems a bit limiting really. vb.net select/case
case(s) are more flexible than c# in this particular case ( no pun
intended ).

In general it is not good for a language to get all possible
features (PL/I, Ada etc. show how that goes).

So features are prioritized. And this one ended up below the
line for C#.

I don't have a problem with that. I think it is relative rare
to want to do that *and* the code are in need not for some cleanup.

Arne
 
Jon said:
They're just different ways of expressing the same thing. Depending on
the ranges involved, the VB compiler may choose to compile it as an
if/else anyway.

Wouldn't it always ?

Or does x86 instruction set have a CASE instruction ?

Arne
 
Arne Vajhøj said:
Wouldn't it always ?

Or does x86 instruction set have a CASE instruction ?

Well, don't forget that the VB compiler isn't compiling to x86 - it's
compiling to IL. (I'm talking about VB9 etc - VB.NET, effectively.)

It sometimes compiles to a switch in IL, and sometimes to if/else.
However, I believe there *is* some JIT support which makes switch
statements faster than if/else in some cases. Basically it uses a
lookup table instead.
 
Just Me,

This is a dead horse, even Jon Skeet writes on his pages that the Select
case from VB for Net is better than the C# switch.

Cor
 
Cor Ligthert said:
Jon,


VB 2002 - VB 2008, VB7 to VB9 are inhouse development codes like Orca and
things like that.

Well, they're not terribly "in house" - things like vb7to8.exe, the
documentation for which includes "Use this tool only on Visual Basic
7.0 or 7.1 source files."

Basically it seems to be reasonably inconsistent :(
 
This is a dead horse, even Jon Skeet writes on his pages that the Select
case from VB for Net is better than the C# switch.

Where exactly do I do that? I can't remember doing so, nor can I find
it on my FAQ page, which is the only page I can remember comparing VB
and C#.

There are certainly issues with C#'s switch statement, but if we were
to allow ranges I'd want a lot more than that at the same time.

Jon
 
Well, they're not terribly "in house" - things like vb7to8.exe, the
documentation for which includes "Use this tool only on Visual Basic
7.0 or 7.1 source files."

Basically it seems to be reasonably inconsistent :(
You know probably who is always fighting in the VB comunity against this
incosistentie

:-)

Cor
 
Jon,

Sorry I thought it was on your pages, probably you have only written this
sometimes.
(And from that I am sure, I don't remember if it was in which newsgroup
anymore).


Cor
 
Cor Ligthert said:
You know probably who is always fighting in the VB comunity against this
incosistentie

The funny thing is that C# is pretty consistent in documentation -
anything with 2003, 2005 2008 etc always refers to the IDE, whereas
versions 1.0, 1.2, 2.0, 3.0 refers to the C# language. (I know, there's
the framework and runtime versions as well...)

However, this doesn't stop some authors from writing books which claim
to teach you "C# 2008". Do an Amazon search and there are loads of
examples. Pah!
 
Cor Ligthert said:
Sorry I thought it was on your pages, probably you have only written this
sometimes.
(And from that I am sure, I don't remember if it was in which newsgroup
anymore).

I've certainly said several times that the switch statement in C# is
one place where the legacy from C shows through - it's slightly better
than in C, but still has issues.

I think there's room in a language for a "test several conditions and
execute the code associated with the first matching one" but I'd want
lots of options - regular expressions on strings, matching of types,
ranges, etc. Groovy does *reasonably* well on this front.

Given that sort of construct, I don't know whether switch would be
worth keeping or not - it's possible that the compiler could recognise
when all the conditions were "simple" mutually exclusive ones and
generate the appropriate code...
 
Cor said:
VB 2002 - VB 2008, VB7 to VB9 are inhouse development codes like Orca
and things like that.

Versions numbers has become a mess.

VS 2008 - C# compiler 3.5 - C# language 3.0
VS 2008 - MS VC++ 9.0 - MS C++ 15.0

Of course VB is messed up.

(and not that Java is better, Java 2 SE 1.4 -> Java 2 SE 5 ->
Java SE 6 is the same mess)

It should be forbidden by law to change names and version numbering
schemes for software.

Arne
 
Jon said:
There are certainly issues with C#'s switch statement, but if we were
to allow ranges I'd want a lot more than that at the same time.

I have always wanted:

switch((a,b))
{
case (1,1):
case (2,2):
//
break;
case (1,2):
//
break;
case (2,1):
//
break;
}

I am not holding my breath ...

Arne
 
Jon Skeet said:
The funny thing is that C# is pretty consistent in documentation -
anything with 2003, 2005 2008 etc always refers to the IDE, whereas
versions 1.0, 1.2, 2.0, 3.0 refers to the C# language. (I know, there's
the framework and runtime versions as well...)

Vb is the same. The language in .NET has been 7, 7.1, 8 and 9. The product
on the other hand has been 2002, 2003, 2005 and 2008.
Visual Studio also uses the year monikers as well as version numbers. The
version numbers for Visual Studio, coincide with the VB language version
numbers ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top