473,796 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ 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 2576
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.co m

"Evan Camilleri" <e7***@yahoo.co .uk.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP09.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.c om> wrote in
message news:%2******** ********@TK2MSF TNGP14.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.c om> wrote in
message news:%2******** ********@TK2MSF TNGP14.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.co m

"Evan Camilleri" <e7***@yahoo.co .uk.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP09.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.co m

"Peter Rilling" <pe***@nospam.r illing.net> wrote in message
news:%2******** ********@TK2MSF TNGP10.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.c om> wrote
in message news:%2******** ********@TK2MSF TNGP14.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.co m

"Evan Camilleri" <e7***@yahoo.co .uk.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP09.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.co m

"Nick Hounsome" <nh***@nickhoun some.me.uk> wrote in message
news:QU******** *************@f e3.news.blueyon der.co.uk...

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:%2******** ********@TK2MSF TNGP14.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******** ********@TK2MSF TNGP09.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******** ******@TK2MSFTN GP15.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2623
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
10912
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 switch-case has a variable (most probably an enumeration) & associated symbols or integral value. Selection is made, base on what symbol/value the variable holds. So
5
3147
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 65536 switch cases. The program would crash when it attempts to run. C++ Compiler does not support above 64K switch cases. I tried to set a limit to 2048 switch cases and 4096 switch cases, but the problem exists when program crashed again. Do...
10
9589
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 'case' code snippet does not. (the code's function is illustrative.) ////////////////////////////////////////// //////// working 'if/else' switch //////// //////////////////////////////////////////
65
6703
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? In terms of generated machine code, how does hundreds of cases in a switch differ from hundreds of if-elses? Do compilers or processors do any optimization on such structured code? Do I need to worry about the performance, usually?
3
19742
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. Now I find myself wanting to use "Select Case" i.e., "switch" in C# regularly, but I always have to find another way b/c C#'s switch statement only allows static or integral variables. For example, I often want to use a switch statement based on the...
11
3160
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 1"); break;
12
12355
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
3364
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 parameters value and signal ID. Signal Id is used to get a user configurable parameter inside a configuration file, which depends on the type of switch. I have implemented it as under. Please ignore those magic numbers as I have mimized logic to...
11
4967
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 statements in switch #2 enter a 3rd switch. I don't receive any compile errors except whenever I attempt to add default switches to switches 2 or 3.
0
9531
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10237
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10187
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10018
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7553
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.