471,327 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,327 software developers and data experts.

switch

I am going from VB.NET to c#. How can I do the following?

switch x
{
case < 10:
do something;
break;
case < 50:
do something;
break;
case < 80:
do something;
break;
default:
do something;
break;
}
Feb 1 '06 #1
14 2436
Evan,

You can't do this in C# with a switch statement, you have to use if
statements, like this:

if (x < 10)
{

}
else
{
if (x < 50)
{
}
else
{
if (x < 80)
{
}
else
{
// default case.
}
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Evan Camilleri" <e7***@yahoo.co.uk.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I am going from VB.NET to c#. How can I do the following?

switch x
{
case < 10:
do something;
break;
case < 50:
do something;
break;
case < 80:
do something;
break;
default:
do something;
break;
}

Feb 1 '06 #2

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...
Evan,

You can't do this in C# with a switch statement, you have to use if
statements, like this:

if (x < 10)
{

}
else
{
if (x < 50)
{
}
else
{
if (x < 80)
{
}
else
{
// default case.
}
}
}


Alternatively you can format it like everyone else does:

if(x< 10)
{
}
else if( x < 50 )
{
}
else if ( x < 80 )
{
}
else
{
}

which is not much more verbose than the switch.
Feb 1 '06 #3
Although you don't really need to next them.

if(...)
{
}
else if(...)
{
}
else if(...)
{
}
else
{
}

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...
Evan,

You can't do this in C# with a switch statement, you have to use if
statements, like this:

if (x < 10)
{

}
else
{
if (x < 50)
{
}
else
{
if (x < 80)
{
}
else
{
// default case.
}
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Evan Camilleri" <e7***@yahoo.co.uk.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I am going from VB.NET to c#. How can I do the following?

switch x
{
case < 10:
do something;
break;
case < 50:
do something;
break;
case < 80:
do something;
break;
default:
do something;
break;
}


Feb 1 '06 #4
No, but it's a matter of personal preference.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Rilling" <pe***@nospam.rilling.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Although you don't really need to next them.

if(...)
{
}
else if(...)
{
}
else if(...)
{
}
else
{
}

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP14.phx.gbl...
Evan,

You can't do this in C# with a switch statement, you have to use if
statements, like this:

if (x < 10)
{

}
else
{
if (x < 50)
{
}
else
{
if (x < 80)
{
}
else
{
// default case.
}
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Evan Camilleri" <e7***@yahoo.co.uk.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I am going from VB.NET to c#. How can I do the following?

switch x
{
case < 10:
do something;
break;
case < 50:
do something;
break;
case < 80:
do something;
break;
default:
do something;
break;
}



Feb 1 '06 #5
Fortunately, I am not everyone else. It's a matter of personal style.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nick Hounsome" <nh***@nickhounsome.me.uk> wrote in message
news:QU*********************@fe3.news.blueyonder.c o.uk...

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP14.phx.gbl...
Evan,

You can't do this in C# with a switch statement, you have to use if
statements, like this:

if (x < 10)
{

}
else
{
if (x < 50)
{
}
else
{
if (x < 80)
{
}
else
{
// default case.
}
}
}


Alternatively you can format it like everyone else does:

if(x< 10)
{
}
else if( x < 50 )
{
}
else if ( x < 80 )
{
}
else
{
}

which is not much more verbose than the switch.

Feb 1 '06 #6
As Nick said, IF ELSE clause will be the only way. SWITCH will not help.

--
Thanks
Vipul Patel
C# MVP www.microsoft.com/mvp
Log bugs at http://msdn.microsoft.com/productfeedback
"Evan Camilleri" <e7***@yahoo.co.uk.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
I am going from VB.NET to c#. How can I do the following?

switch x
{
case < 10:
do something;
break;
case < 50:
do something;
break;
case < 80:
do something;
break;
default:
do something;
break;
}

Feb 1 '06 #7
I hate when VB does stuff better than C#. I wonder why switch is so limited
in C#, anyone knows why?

I also miss the 'with' keyword. It was in Delphi and I can't see why Mr.
Hejlsberh don't want it in C#?
- Michael S
Feb 1 '06 #8

"Michael S" <no@mail.com> wrote in message
news:Od**************@TK2MSFTNGP15.phx.gbl...
I hate when VB does stuff better than C#. I wonder why switch is so limited
in C#, anyone knows why?

Well, there are performance benefits(a switch with only one value can easily
be transformed into a jump table, for example), but largely I think it was a
matter of sticking with tradition.
I also miss the 'with' keyword. It was in Delphi and I can't see why Mr.
Hejlsberh don't want it in C#?

Because it sucks. :)

This has been hashed over many, many times. While it once had significant
performance benefits in VB, most find that it doesn't offer anything but
minor cosmetic changes(and not improvement in everyones mind), that it is
easy to abuse, somewhat difficult to fit into the language and maintain
consistency, and that its potential for abuse outweighs its very limited
value. A quick Google Groups search brings up a number of threads:

http://groups.google.com/groups?q=gr...ith+keyword%22

Scott Wiltamuth addressed this on a gotdotnet column quite some time ago:
http://www.gotdotnet.com/team/csharp.../ask.aspx#with

I personally don't like 'with' and am quite happy its not in the language.
Feb 1 '06 #9
> This has been hashed over many, many times. While it once had significant
performance benefits in VB, most find that it doesn't offer anything but
minor cosmetic changes(and not improvement in everyones mind), that it is
easy to abuse, somewhat difficult to fit into the language and maintain
consistency, and that its potential for abuse outweighs its very limited
value.


Sure, But operator overloading is there, and it can be abused.

I can write an abstract class, subclass it into two clasess, having one of
their mothods staying virtual, while reintroducing another one. In a couple
of subclasses to the reintroduced one, I could just make one of these
methods virtual again. And start subclassing all again yet again... Wanna
maintain it for me? Is that abuse?

Also I could code a property like this

public int Count
{
get
{
MyCode.FormatC();
return 0;
}
}

Isn't that abuse?

No! It is poor communication skills!

If I found it, I'd make the property Count into a method and rename it to
FormatC();

And about sugar, the same could be said for the 'using' keyword. Why not
just use try finally and call dispose?

Heck, who needs properties, why not just use get and set methods.... We
don't need no stinking delegates and events and are happy with the
Listener-pattern. Period!.. Adhere to the gang of four by default or we have
to get you assimilated. Resistance is futile! =)

Oh, why do we bother with all this sugar? We can do Vtables in assembler if
we wanted. Why not just push, mov and pop?

While 'with' was clumsy in Delphi (poor intellisense) and retarded in VB (no
scope) it could be highly useful in .NET. And it's more than just sugar...

MyClass o = new MyClass()
with(o)
{
o. //<- only members of MyClass shows in intellicense in visual studio.
int someNumber = o.x + o.y; //someNumber in a local scope.
o.z = someNumber;
}
someNumber = 0; // compile-time error. No such thing in scope.

I would like to see the above in C#. Makes for better maintainance.
If you don't want to use it, then don't. But I kinda like how C# gets
bloated... While not going C++

Cite:

Whenever the C++ language designers had two competing ideas as to how they
should solve some problem, they said, "OK, we'll do them both". So the
language is too baroque for my taste. (Donald E Knuth)

I agree with Knuth fully in this (like he would care what I think). But I
also think that a 'with'-statement and a more flexible switch could do
wonders for C#, and stying a clean language, without going 'baroque'...

My 2^2^2 cents....

Happy Coding
- Michael S

ps.

I like qutoes, and here comes some more on C++...

If you think C++ is not overly complicated, just what is a protected
abstract virtual base pure virtual private destructor and when was the last
time you needed one? (Tom Cargill)
I invented the term Object-Oriented, and I can tell you I did not have C++
in mind. (Alan Kay)

If C++ has taught me one thing, it's this: Just because the system is
consistent doesn't mean it's not the work of Satan. (Andrew Plotkin)

Within C++, there is a much smaller and cleaner language struggling to get
out. (Bjarne Stroustrup)

All C programs do the same thing: look at a character and do nothing with
it. (Peter Weinberger)

To me C++ seems to be a language that has sacrificed orthogonality and
elegance for random expediency. (Meilir Page-Jones)

Unix and C are the ultimate computer viruses. (Richard P Gabriel)

C++ is like jamming a helicopter inside a Miata and expecting some sort of
improvement. (Drew Olbrich)

C++: Simula in wolf's clothing. (Bjarne Stroustrup)


Feb 2 '06 #10

"Michael S" <no@mail.com> wrote in message
news:up****************@TK2MSFTNGP12.phx.gbl...
This has been hashed over many, many times. While it once had significant
performance benefits in VB, most find that it doesn't offer anything but
minor cosmetic changes(and not improvement in everyones mind), that it is
easy to abuse, somewhat difficult to fit into the language and maintain
consistency, and that its potential for abuse outweighs its very limited
value.
Sure, But operator overloading is there, and it can be abused.


You miss the point. It is not that it can be abused (virtually everything
can be,) its that the value it adds is significantly lower than the language
complexity(at the compiler level and at the human level) and the probability
for abuse. With is one of those features that is probably abused a dozen
times for every time it offers a single benefit. With does nothing that 10
more characters couldn't, but it takes up quite a bit of compiler space and
is very abusable.
And about sugar, the same could be said for the 'using' keyword. Why not
just use try finally and call dispose? Using offers a nice shortcut that provides benefit, promotes correct code,
and improves readability(using is more succienct and simpler than a
try\finally.) With, on the other hand, lets lazy programmers write lazy
code. It has no other benefit. No performance boost, no additional logic, no
improved correctness.

Heck, who needs properties, why not just use get and set methods.... We
don't need no stinking delegates and events and are happy with the
Listener-pattern. Period!.. Adhere to the gang of four by default or we
have to get you assimilated. Resistance is futile! =)
Same as above. It improves readability and actually adds simplicity. With,
on the other hand, does neither.

Oh, why do we bother with all this sugar? We can do Vtables in assembler
if we wanted. Why not just push, mov and pop? Indeed. Why not add a keyword to do every possible thing you could imagine:
returntrueifvariableqisblue;

There has to the a point where you start and one where you stop. The goal of
any language is to add features that provide true enhancement, not ones
people think are sexy because they used them elsewhere.

While 'with' was clumsy in Delphi (poor intellisense) and retarded in VB
(no scope) it could be highly useful in .NET. And it's more than just
sugar...

MyClass o = new MyClass()
with(o)
{
o. //<- only members of MyClass shows in intellicense in visual studio.
int someNumber = o.x + o.y; //someNumber in a local scope.
o.z = someNumber;
}
someNumber = 0; // compile-time error. No such thing in scope.
Much of this already exists, actually. It is provided for in one of the very
base language structures, the code block.

MyClass o = new MyClass();
{
int someNUmber = o.x+o.y;
o.z = someNumber;
}

someNumber = 0; //compile-time error. No such variable in context.

What you mean by only "only members of MyClass shows in intellicense in
visual studio." Do you mean only those defined on MyClass? In which case I
disagree with you as constraining a type to only those methods defined on a
particular generation is terribly fragile and confusing in my mind, you'd
lose such basics as ToString(). And if you don't mean that, then I see no
benefit. Could you explain what exactly you think it would offer in more
detail?

I would like to see the above in C#. Makes for better maintainance.
If you don't want to use it, then don't. But I kinda like how C# gets
bloated... While not going C++
The "don't like it, don't use it" argument is a really bad one. Few people
have total control over what they have to use. Other people write code, most
people have to deal with what other people do. Far more people are stuck
with what other people decide than those that can chose for themselves.
Every feature has to be considered for every person, or you will screw it
up. No one can just ignore any one feature and realistically never encounter
it, let alone not encounter it on a relativly comon basis.

Cite:

Whenever the C++ language designers had two competing ideas as to how they
should solve some problem, they said, "OK, we'll do them both". So the
language is too baroque for my taste. (Donald E Knuth)

I agree with Knuth fully in this (like he would care what I think). But I
also think that a 'with'-statement and a more flexible switch could do
wonders for C#, and stying a clean language, without going 'baroque'...


A stronger switch would be nice. With would be foolish. If you care about my
opinion, anyway.
Feb 2 '06 #11
I've never seen a coding standard that agreed with you.

I'm not volounteering for the thought police but I have honestly never met
another experienced programmer who nested stuff like that.

That's not a criticism of you - but if you want the maximum number of people
to understand your code easily then I think you should change.

Feel free to point me to any online poll showing me the error of my ways.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:un**************@TK2MSFTNGP14.phx.gbl...
Fortunately, I am not everyone else. It's a matter of personal style.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nick Hounsome" <nh***@nickhounsome.me.uk> wrote in message
news:QU*********************@fe3.news.blueyonder.c o.uk...

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:%2****************@TK2MSFTNGP14.phx.gbl...
Evan,

You can't do this in C# with a switch statement, you have to use if
statements, like this:

if (x < 10)
{

}
else
{
if (x < 50)
{
}
else
{
if (x < 80)
{
}
else
{
// default case.
}
}
}


Alternatively you can format it like everyone else does:

if(x< 10)
{
}
else if( x < 50 )
{
}
else if ( x < 80 )
{
}
else
{
}

which is not much more verbose than the switch.


Feb 2 '06 #12
> While 'with' was clumsy in Delphi

Delphi's implementation wasn't 'clumsy', it was downright dangerous and wise
programmers refused to use it. VB's with was rather better.
Feb 2 '06 #13
"Michael S" <no@mail.com> wrote in message
news:Od**************@TK2MSFTNGP15.phx.gbl...
I hate when VB does stuff better than C#. I wonder why switch is so limited in C#, anyone knows why?


You're looking at it from the wrong point of view. When C was being
designed (in the 1970's), they added if() to the language, but they wanted a
way to do something which could not be easily done with nested if()s,
specifically, a jump table (and other optimizations). So, they created the
switch statement. When C begat C++ and when C++ begat C#, the syntax was
carried forward, but more importantly, the functionality was also carried
forward.

Years later, the VB designers though C's switch statement looked cool,
and decided to add it to VB (renaming it Select) (actually I think it
entered the language in the MSDOS/QuickBasic era). However, it is purely
cosmetic --- VB's Select does *nothing* that nested IFs don't do -- It
probably even compiles to identical code.

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Feb 3 '06 #14

"James Curran" <ja*********@mvps.org> wrote in message
news:ex**************@TK2MSFTNGP11.phx.gbl...
"Michael S" <no@mail.com> wrote in message
news:Od**************@TK2MSFTNGP15.phx.gbl...
I hate when VB does stuff better than C#. I wonder why switch is so

limited
in C#, anyone knows why?


You're looking at it from the wrong point of view. When C was being
designed (in the 1970's), they added if() to the language, but they wanted
a
way to do something which could not be easily done with nested if()s,
specifically, a jump table (and other optimizations). So, they created
the
switch statement. When C begat C++ and when C++ begat C#, the syntax was
carried forward, but more importantly, the functionality was also carried
forward.

Years later, the VB designers though C's switch statement looked cool,
and decided to add it to VB (renaming it Select) (actually I think it
entered the language in the MSDOS/QuickBasic era). However, it is purely
cosmetic --- VB's Select does *nothing* that nested IFs don't do -- It
probably even compiles to identical code.


One would think that a clever compiler designer would be able to dream up a
switch that can do either, they've already added string support to the C#
version which isn't quite a jump table(it does use a hashtable at some point
I think) and multiple labels. Allowing range shortcuts, if not whole
expressions, at some acceptable level of performance shouldn't be impossible
in the modern day.
Feb 3 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Rudi Hansen | last post: by
10 posts views Thread by Myster Ious | last post: by
5 posts views Thread by Bryan Parkoff | last post: by
10 posts views Thread by clueless_google | last post: by
65 posts views Thread by He Shiming | last post: by
3 posts views Thread by pgraeve | last post: by
11 posts views Thread by ME | last post: by
12 posts views Thread by | last post: by
11 posts views Thread by =?Utf-8?B?anAybXNmdA==?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.