切换案例是真的 [英] switch case with true

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

问题描述

为什么这样的工作不合适?我得到一个恒定值是

预期所有3个案例陈述。


私有DataGrid CurrentDataGrid(DataGrid firstDg,DataGrid secondDg)

{

试试

{

开关(true)

{

case firstDg.Items.Count!= 0& secondDg.Items.Count!= 0:

抛出新的异常();

break;

case firstDg.Items.Count!= 0 :

返回firstDg;

case secondDg.Items.Count!= 0:

返回secondDg;

默认:

返回null;

}

}

catch

{

返回null;

}

}

解决方案

< blockquote> C#case语句中的值必须是常量(在编译时),而你

正在使用诸如firstDg.Items.Count之类的变量。 VB.NET的案例陈述

(或VB6)没有这样的限制,以防你来自

背景。


-


Carlos J. Quintero


MZ-Tools 4.0:Visual Studio .NET的生产力插件

您可以更快地编码,设计和记录。
http:// www .mztools.com

" Eric S." <莫***** @ hotmail.com> escribióenel mensaje

news:11 ********************** @ c13g2000cwb.googlegr oups.com ...

为什么这样的工作不合适?我得到一个恒定的值是预期的。在所有3个案例陈述中。

私有DataGrid CurrentDataGrid(DataGrid firstDg,DataGrid secondDg)
{
尝试
{
switch(true)
{
case firstDg.Items.Count!= 0& secondDg.Items.Count!= 0:
抛出新的异常();
break;
case firstDg.Items.Count!= 0:
返回firstDg;
case secondDg.Items.Count!= 0:
返回secondDg;
默认值:
返回null;
}
}
catch
{
返回null;
}
}



Eric S.写道:

为什么这不是什么工作吗?我得到一个恒定的值是预期的。在所有3个案例陈述中。



< snip>


让我们说这个例子是可编译的。如果其中两个编译成真,

那么呢?我知道这不是你的例子中的有效案例,但

编译器需要保证它只需要一个分支,并添加

静态表达式分析到这种情况会让编译器很难保证这一点。


switch的语法要求你使用常量来表示情况,所以

这就是你必须要做的事情。在你的情况下,我会带几张

if语句。


-

Lasse V?gs?ther Karlsen
http://www.vkarlsen.no/

mailto:la *** @ vkarlsen.no

PGP KeyID:0x0270466B




你可能会收到一个警告,说一些关于恒定值的信息

用于开关


案例标签应该是常量,它不能是需要在运行时进行求值的表达式,它的值需要在编译时知道为编译器生成跳转指令。


在你的情况下你可以使用几个如果cnostructions。

干杯,


-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

佛罗里达州交通局


Eric S. <莫***** @ hotmail.com>在消息中写道

news:11 ********************** @ c13g2000cwb.googlegr oups.com ...

为什么这样的工作不合适?我得到一个恒定的值是预期的。在所有3个案例陈述中。

私有DataGrid CurrentDataGrid(DataGrid firstDg,DataGrid secondDg)
{
尝试
{
switch(true)
{
case firstDg.Items.Count!= 0& secondDg.Items.Count!= 0:
抛出新的异常();
break;
case firstDg.Items.Count!= 0:
返回firstDg;
case secondDg.Items.Count!= 0:
返回secondDg;
默认值:
返回null;
}
}
catch
{
返回null;
}
}



Why doesn''t something like this work? I get "a constant value is
expected" on all 3 case statements.

private DataGrid CurrentDataGrid(DataGrid firstDg, DataGrid secondDg)
{
try
{
switch (true)
{
case firstDg.Items.Count != 0 & secondDg.Items.Count != 0:
throw new Exception();
break;
case firstDg.Items.Count != 0:
return firstDg;
case secondDg.Items.Count != 0:
return secondDg;
default:
return null;
}
}
catch
{
return null;
}
}

解决方案

The values in C# case statements must be constants (at compile-time) and you
are using variables such as firstDg.Items.Count. Case statements of VB.NET
(or VB6) do not have such restriction, just in case you come from that
background.

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
"Eric S." <mo*****@hotmail.com> escribió en el mensaje
news:11**********************@c13g2000cwb.googlegr oups.com...

Why doesn''t something like this work? I get "a constant value is
expected" on all 3 case statements.

private DataGrid CurrentDataGrid(DataGrid firstDg, DataGrid secondDg)
{
try
{
switch (true)
{
case firstDg.Items.Count != 0 & secondDg.Items.Count != 0:
throw new Exception();
break;
case firstDg.Items.Count != 0:
return firstDg;
case secondDg.Items.Count != 0:
return secondDg;
default:
return null;
}
}
catch
{
return null;
}
}



Eric S. wrote:

Why doesn''t something like this work? I get "a constant value is
expected" on all 3 case statements.


<snip>

Let''s say the example was compilable. If two of those compiled to true,
what then ? I know that that''s not a valid case in your example, but the
compiler needs to guarantee that it only takes one branch, and adding
static expression analysis to this case would make it really hard for
the compiler to guarantee that.

The syntax for switch requires you to use constants for the cases, so
that''s what you have to do. In your case I would go with a couple of
if-statements.

--
Lasse V?gs?ther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x0270466B


Hi,

and you may getting also a warning, saying something about a constant value
being used in a switch

the case(s) labels should be constants, it cannot be an expression that need
to be evaluated in runtime, its values need to be know at compile time as
the compiler generate the jumps instructions.

In your case you can use several if cnostructions.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Eric S." <mo*****@hotmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...

Why doesn''t something like this work? I get "a constant value is
expected" on all 3 case statements.

private DataGrid CurrentDataGrid(DataGrid firstDg, DataGrid secondDg)
{
try
{
switch (true)
{
case firstDg.Items.Count != 0 & secondDg.Items.Count != 0:
throw new Exception();
break;
case firstDg.Items.Count != 0:
return firstDg;
case secondDg.Items.Count != 0:
return secondDg;
default:
return null;
}
}
catch
{
return null;
}
}



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

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