I am a csharp newbie and am trying to work through some code and have gotten
myself stuck in a loop. Any help would be appreciated.
I have the following code in place:
public IDcCommand GetCommand(EnumProviders provider)
{
switch( provider )
{
case EnumProviders.ODBC:
return new OdbcCommand();
case EnumProviders.SQLClient:
return new SqlCommand();
case EnumProviders.OLEDB():
return new OleDbCommand();
}
}
If I leave the code as is, I get the error "Control cannot fall through from
one case label ('case 0:') to another". If I change each case to add a break
after the return line, then I get an unreachable code detected error. How
can I get this code to work? I thought a return was supposed to a value exit
from a fall through. Any help would be most appreciated.
Thanks,
Steve Bering 9 3129
Steve, case EnumProviders.OLEDB():
Remove the parentheses on this line.
Mattias
--
Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Yes, I made that mistake, but that was just in the post. Any ideas of the
switch issue?
Thanks,
Steve Bering
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:ey**************@TK2MSFTNGP11.phx.gbl... Steve,
case EnumProviders.OLEDB():
Remove the parentheses on this line. Mattias
-- Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ Please reply only to the newsgroup.
Add break statements after your case condition and you won't 'fall thru'
from one to the other.
EX:
switch (condition)
{
case <test-condition>:
//do something
break; //this is what you are missing
case <test-condition>:
//something
break;
}
"Steve Bering" <st*********@hotmail.com> wrote in message
news:us**************@TK2MSFTNGP12.phx.gbl... I am a csharp newbie and am trying to work through some code and have
gotten myself stuck in a loop. Any help would be appreciated. I have the following code in place:
public IDcCommand GetCommand(EnumProviders provider) { switch( provider ) { case EnumProviders.ODBC: return new OdbcCommand(); case EnumProviders.SQLClient: return new SqlCommand(); case EnumProviders.OLEDB(): return new OleDbCommand(); } }
If I leave the code as is, I get the error "Control cannot fall through
from one case label ('case 0:') to another". If I change each case to add a
break after the return line, then I get an unreachable code detected error. How can I get this code to work? I thought a return was supposed to a value
exit from a fall through. Any help would be most appreciated.
Thanks,
Steve Bering
Steve, Yes, I made that mistake, but that was just in the post. Any ideas of the switch issue?
I assume that "IDcCommand" should actually be "IDbCommand" then?
It would really help if you posted your actual code, so we don't have
to guess what's wrong based on some code snippet that might not even
have the same problem in it.
Mattias
--
Mattias Sjögren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Opps , sorry just re-read your post ...
In a similiar example from some of my code, I am repeating your condition
but receive no error when set up like this:
switch (formatString)
{
case "F":
return String.Format("RequestID: {0}",this.RequestID);
case "S":
return String.Format("DisplayString: {0}",this.DisplayString);
}
"Trebek" <tr****@intheformofaquestion.com> wrote in message
news:3f***********************@nnrp.fuse.net... Add break statements after your case condition and you won't 'fall thru' from one to the other.
EX:
switch (condition) { case <test-condition>: //do something break; //this is what you are missing
case <test-condition>: //something break;
} "Steve Bering" <st*********@hotmail.com> wrote in message news:us**************@TK2MSFTNGP12.phx.gbl... I am a csharp newbie and am trying to work through some code and have gotten myself stuck in a loop. Any help would be appreciated. I have the following code in place:
public IDcCommand GetCommand(EnumProviders provider) { switch( provider ) { case EnumProviders.ODBC: return new OdbcCommand(); case EnumProviders.SQLClient: return new SqlCommand(); case EnumProviders.OLEDB(): return new OleDbCommand(); } }
If I leave the code as is, I get the error "Control cannot fall through from one case label ('case 0:') to another". If I change each case to add a break after the return line, then I get an unreachable code detected error.
How can I get this code to work? I thought a return was supposed to a value exit from a fall through. Any help would be most appreciated.
Thanks,
Steve Bering
Steve Bering wrote: I am a csharp newbie and am trying to work through some code and have gotten myself stuck in a loop. Any help would be appreciated. I have the following code in place:
public IDcCommand GetCommand(EnumProviders provider) { switch( provider ) { case EnumProviders.ODBC: return new OdbcCommand(); case EnumProviders.SQLClient: return new SqlCommand(); case EnumProviders.OLEDB(): return new OleDbCommand(); } }
If I leave the code as is, I get the error "Control cannot fall through from one case label ('case 0:') to another". If I change each case to add a break after the return line, then I get an unreachable code detected error. How can I get this code to work? I thought a return was supposed to a value exit from a fall through. Any help would be most appreciated.
Thanks,
Steve Bering
IDcCommand result;
case EnumBroviders.ODBC:
result = new OdbcCommand();
etc.
It's IMO not good coding practice to exit in mid function like this.
Steve Bering <st*********@hotmail.com> wrote: Yes, I made that mistake, but that was just in the post. Any ideas of the switch issue?
Please post the *actual* code, and the *exact* error message.
The error message I'd *expect* you to get with code like the piece you
posted is "Not all code paths return a value."
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Paul Robson <au******@autismuk.muralichucks.freeserve.co.uk> wrote: IDcCommand result;
case EnumBroviders.ODBC: result = new OdbcCommand();
etc.
It's IMO not good coding practice to exit in mid function like this.
Whereas I'd consider it actually to be more readable to return as soon
as you know exactly what you're going to return. Why delay it?
There are various cases where it *does* make sense to try to have a
single exit point, but I'd say this isn't one of them.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
On Wed, 19 Nov 2003 22:13:06 +0000, Paul Robson
<au******@autismuk.muralichucks.freeserve.co.uk> wrote:
<snip> It's IMO not good coding practice to exit in mid function like this.
Rubbish.
Single entry/single exit is a paradigm that has its place but not
in the "general coding practice" regimen.
Oz This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jamie Wright |
last post by:
I have using a 'switch' to retrieve the name of the website section through
sending a 'section' integer...
function title($section)
{
switch ($section)
{
case 0:
$output = "Introduction";...
|
by: Wolfgang Uhr |
last post by:
Hello
I have a small Problem by using the mail() command from php. To surround
the problem, I do the following:
1. I go to the server and send me a mail using the linux-mail-Command.
In...
|
by: mb12036 |
last post by:
All-
Having a problem installing a DB2 client on a machine running AIX
version 5.0. Client appeared to install one time succesfully, then
was uninstalled and a reinstall was attempted. For...
|
by: Phoe6 |
last post by:
The Program Fragment is this:
int choice; /* Users Input Command */
..
..
..
printf("Enter a command: ");
fflush(stdin);
choice = getc(stdin);
printf("\n");
|
by: Leandro Berti via DotNetMonster.com |
last post by:
Hi All,
I wrote a code to do serial communication with an equipament.
When i use the code outside of threaded class it seens work properly,
but when i put inside a class and execute a thread in...
|
by: marti |
last post by:
I've run into three problems trying to solve this one issue.
Using VS2005 B2 & sql server 2000, I create a sqldatasource and bind a
gridview to it. Everything looks good. However, if there is an...
|
by: Peter Kirk |
last post by:
Hi
i have a string variable which can take one of a set of many values.
Depending on the value I need to take different (but related) actions. So I
have a big if/else-if construct - but what is...
|
by: brianisu |
last post by:
I'm using VB6 to communicate with a weigh scale and am having trouble getting it to respond to my commands. The scale is hooked up using a RS232 to the COM port .
Using Hyperterminal I can...
|
by: JahMic |
last post by:
I'm having a problem with exec on my hosting server. Unfortunately,
the hosting support seems to be anything but helpful.
The following works fine on my localhost:
<?php
$MaskData =...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |