Problems with switch command

S

Steve Bering

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
 
S

Steve Bering

Yes, I made that mistake, but that was just in the post. Any ideas of the
switch issue?

Thanks,

Steve Bering
 
M

Mattias Sjögren

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
 
T

Trebek

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;

}
 
T

Trebek

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);


}
 
P

Paul Robson

Steve said:
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.
 
J

Jon Skeet [C# MVP]

Steve Bering said:
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."
 
J

Jon Skeet [C# MVP]

Paul Robson said:
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.
 
O

ozbear

On Wed, 19 Nov 2003 22:13:06 +0000, Paul Robson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top