473,796 Members | 2,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

switch statement bug

I tested following program gives runtime error:
Unhandled Exception: System.InvalidP rogramException :
Common Language Runtime detected an invalid program.

using System;
public class SwitchTest
{
static void Main()
{
const int i = 1;
switch (i)
{
case 0:
Console.WriteLi ne
(0);
goto default;
case 1:
Console.WriteLi ne
(1);
goto case 0;
default:
Console.WriteLi ne
("all others");
break;
}
}
}

However, by rearranging the case section, this bug is
avoided:

public class SwitchTest
{
static void Main()
{
const int i = 1;
switch (i)
{
case 1:
Console.WriteLi ne
(1);
goto case 0;
case 0:
Console.WriteLi ne
(0);
goto default;
default:
Console.WriteLi ne
("all others");
break;
}
}
}

According to documentation, since C# has a "no fall
through" rule to ensure the order of case doesn't matter,
I think this is a bug.
Nov 15 '05 #1
2 1482
Fall through definitely isn't supported - to prove this, leave a break
statement out of any of the cases and it won't compile.

I think if you switch it to int i = 1 instead of const that you won't have a
problem. The fact that there isn't fall through doesn't necessarily mean
that the ordering doesn't matter. Logically I think that's true, but
architecturally , I'm not sure that's so b/c if you take out the const, you
don't have a problem.
"fred" <a7******@telus .net> wrote in message
news:01******** *************** *****@phx.gbl.. .
I tested following program gives runtime error:
Unhandled Exception: System.InvalidP rogramException :
Common Language Runtime detected an invalid program.

using System;
public class SwitchTest
{
static void Main()
{
const int i = 1;
switch (i)
{
case 0:
Console.WriteLi ne
(0);
goto default;
case 1:
Console.WriteLi ne
(1);
goto case 0;
default:
Console.WriteLi ne
("all others");
break;
}
}
}

However, by rearranging the case section, this bug is
avoided:

public class SwitchTest
{
static void Main()
{
const int i = 1;
switch (i)
{
case 1:
Console.WriteLi ne
(1);
goto case 0;
case 0:
Console.WriteLi ne
(0);
goto default;
default:
Console.WriteLi ne
("all others");
break;
}
}
}

According to documentation, since C# has a "no fall
through" rule to ensure the order of case doesn't matter,
I think this is a bug.

Nov 15 '05 #2
fred wrote:
I tested following program gives runtime error:
Unhandled Exception: System.InvalidP rogramException :
Common Language Runtime detected an invalid program.

using System;
public class SwitchTest
{
static void Main()
{
const int i = 1;
switch (i)
{
case 0:
Console.WriteLi ne
(0);
goto default;
case 1:
Console.WriteLi ne
(1);
goto case 0;
default:
Console.WriteLi ne
("all others");
break;
}
}
}


Apparently, this bug has been fixed in Framework version 1.1.

--
mikeb

Nov 15 '05 #3

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

Similar topics

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
35
8360
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except for '5'. So in his switch statement, he omits a case for '5':
17
2827
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can anybody point me to the innards of it please? Sorry if this is OT on clc and more relevant to comp.compilers. I am confused here too.
5
6109
by: Alvin Bruney | last post by:
Is a switch more efficient than an if statement? I observe thru the debugger that a switch statement jumps directly to its case handler where as an if statement examines all conditions sequentially. Is that a trick of the debugger or is a switch quicker by O(n).
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...
27
5640
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
25
18393
by: v4vijayakumar | last post by:
'continue' within switch actually associated with the outer 'while' loop. Is this behavior protable? int ch = '\n'; while (true) { switch(ch) { case '\n': cout << "test"; continue; } }
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;
2
1460
by: Phillip B Oldham | last post by:
What would be the optimal/pythonic way to subject an object to a number of tests (based on the object's attributes) and redirect program flow? Say I had the following: pets = {'name': 'fluffy', 'species': 'cat', 'size': 'small'} pets = {'name': 'bruno', 'species': 'snake', 'size': 'small'} pets = {'name': 'rex', 'species': 'dog', 'size': 'large'}
13
11854
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so just curious to find out is it effective to use this method?? or is there is any other alternative method present so that execution time and code size can be reduced?? Thanks in advance.
0
9685
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
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...
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...
0
6795
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
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.