473,805 Members | 1,995 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
14 2579

"Michael S" <no@mail.com> wrote in message
news:up******** ********@TK2MSF TNGP12.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(usi ng 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:
returntrueifvar iableqisblue;

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.c om> wrote in
message news:un******** ******@TK2MSFTN GP14.phx.gbl...
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 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******** ******@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?


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*********@mv ps.org> wrote in message
news:ex******** ******@TK2MSFTN GP11.phx.gbl...
"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?


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
2624
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
10913
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
9591
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
6704
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
19743
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
12361
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
4969
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
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10607
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
9182
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
7645
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...
0
6875
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5541
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4317
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 we have to send another system
3
3007
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.