473,465 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2539
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Rudi Hansen | last post by:
I dont seem to be able to find the switch statement in Python. I would like to be able to do switch(var) case 1 : print "var = 1" case 2: print "var = 2"
10
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
5
by: Bryan Parkoff | last post by:
C++ programmers and I tried to experience by writing 65536 switch cases. It is too large for Windows XP and other operating system to handle. It looks like that JMP Table obtains 256K bytes for...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
65
by: He Shiming | last post by:
Hi, I just wrote a function that has over 200 "cases" wrapped in a "switch" statement. I'm wondering if there are performance issues in such implementation. Do I need to optimize it some way? ...
3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
11
by: ME | last post by:
In C# the following code generates a compiler error ("A constant value is expected"): public void Test(string value) { switch (value) { case SimpleEnum.One.ToString(): MessageBox.Show("Test...
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
7
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with...
11
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Can switch statements be nested? I've got a large routine that runs off of a switch statement. If one of the switches in switch #1 is true, that enters switch statement #2. Some of the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.