Problems with switch command

  • Thread starter Thread starter Steve Bering
  • Start date Start date
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
 
Yes, I made that mistake, but that was just in the post. Any ideas of the
switch issue?

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

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


}
 
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.
 
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."
 
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.
 
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
 
Back
Top