切换命令有问题 [英] Problems with switch command

查看:52
本文介绍了切换命令有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个csharp新手,我正在尝试通过一些代码并且已经得到了

我自己被困在一个循环中。任何帮助将不胜感激。

我有以下代码:


公共IDcCommand GetCommand(EnumProviders提供商)

{

开关(提供商)

{

案例EnumProviders.ODBC:

返回新的OdbcCommand();

case EnumProviders.SQLClient:

返回新的SqlCommand();

case EnumProviders.OLEDB():

返回new OleDbCommand();

}

}


如果我按原样保留代码,我会收到错误Control not not从

一个案例标签(''案例0:'')到另一个案件标签。如果我改变每个案例在返回行之后添加一个休息

,那么我得到一个无法访问的代码检测错误。怎么

我可以让这个代码工作吗?我认为回报应该是价值退出

。任何帮助都将非常感激。


谢谢,


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

推荐答案

Steve,
Steve,
case EnumProviders.OLEDB():
case EnumProviders.OLEDB():




删除此行的括号。


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/

请回复新闻组。



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.


是的,我犯了这个错误,但那只是在帖子中。

开关问题的任何想法?


谢谢,


Steve Bering

" Mattias Sj?gren" <毫安******************** @ mvps.org>在消息中写道

新闻:ey ************** @ TK2MSFTNGP11.phx.gbl ...
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,
Steve,
case EnumProviders.OLEDB():
case EnumProviders.OLEDB():



删除此行上的括号。

Mattias

-
Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
请仅回复新闻组。



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.



添加在你的情况发生之后打破陈述并且你不会从一个到另一个掉落'




EX:


开关(条件)

{

case< test-condition>:

//做某事

休息; //这就是你所缺少的


case< test-condition>:

//这个东西

break;


}

" Steve Bering" < ST ********* @ hotmail.com>在留言中写道

新闻:我们************** @ TK2MSFTNGP12.phx.gbl ...
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...
我是csharp新手并且我正在尝试通过一些代码并让
陷入循环中。任何帮助将不胜感激。
我有以下代码:

公共IDcCommand GetCommand(EnumProviders提供商)
{
开关(提供商)
{
案例EnumProviders.ODBC:
返回新的OdbcCommand();
案例EnumProviders.SQLClient:
返回新的SqlCommand();
案例EnumProviders.OLEDB():
返回新的OleDbCommand();
}


如果我按原样保留代码,我会收到错误控制不能通过
从一个案例标签(''案例0:'')到另一个案例标签。如果我更改每个案例以在返回行后添加
中断,那么我会收到无法访问的代码检测错误。如何才能让这段代码运行起来?我认为回报应该是价值
退出。任何帮助都将不胜感激。

谢谢,

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



这篇关于切换命令有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆