?和:用于v2中的条件表达式 [英] ? and : for conditional expressions in v2

查看:53
本文介绍了?和:用于v2中的条件表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




在v1.x中,以下代码完美运行:


mobjSqlParameter.Value = strPayerBusinessName == null? strPayerBusinessName

:(对象)DBNull.Value;


但是,我无法在v2中使用它。我知道

strPayerBusinessName的值为null,因为如果我在立即窗口中写了


strPayerBusinessName == null,它将返回true。


但是,表达式似乎没有评估右侧,没有

我写的东西。例如,以下代码


mobjSqlParameter.Value = strPayerBusinessName == null? strPayerBusinessName

:" Hello" ;;


不会使用字符串Hello填充mobjSqlParameter.Value。什么时候

strPayerBusinessName为空。


这个语法在v2中是否被弃用?


当然我意识到,我可以写:


if(strPayerBusinessName == null)

{

mobjSqlParameter.Value = DBNull.Value;

}

其他

{

mobjSqlParameter.Value = strPayerBusinessName;

}


但我很好奇为什么? :语法似乎不再起作用了。


感激不尽的任何帮助。


Mark

Hi,

In v1.x, the following code worked perfectly:

mobjSqlParameter.Value = strPayerBusinessName == null ? strPayerBusinessName
: (object)DBNull.Value;

However, I can''t get it to work in v2. I know that the value of
strPayerBusinessName is null because if I write

strPayerBusinessName == null in the Immediate Window, it returns "true".

However, the expression does not seem to evaluate the right-hand side, no
matter what I write. E.g., the following code

mobjSqlParameter.Value = strPayerBusinessName == null ? strPayerBusinessName
: "Hello";

does not populate mobjSqlParameter.Value with the string "Hello" when
strPayerBusinessName is null.

Has this syntax been deprecated in v2?

I realise, of course, that I can write:

if(strPayerBusinessName == null)
{
mobjSqlParameter.Value = DBNull.Value;
}
else
{
mobjSqlParameter.Value = strPayerBusinessName;
}

but I''m curious as to why the ? : syntax doesn''t seem to work any more.

Any assistance gratefully received.

Mark

推荐答案

?:工作原理与以往一样,但是你注意到你使用了

错误的比较吗?当名字为空时你正在返回名字,而我不会认为这是你打算做的!使用!=代替==或

否则反转:分支。

-
http://www.kynosarges.de
?: works the same as ever, but did you notice that you''re using the
wrong comparison? You''re returning Name when Name is null, and I
don''t think that''s what you intend to do! Use != instead of == or
else reverse the : branches.
--
http://www.kynosarges.de


Mark Rae< ma ** @ markN- OSPAM.co.uk>写道:
Mark Rae <ma**@markN-O-S-P-A-M.co.uk> wrote:
在v1.x中,以下代码完美运行:

mobjSqlParameter.Value = strPayerBusinessName == null? strPayerBusinessName
:(对象)DBNull.Value;

然而,我无法在v2中使用它。我知道
strPayerBusinessName的值为null,因为如果我在立即窗口中写了

strPayerBusinessName == null,它将返回true。

然而,表达似乎没有评估右侧,不是我写的东西。例如,以下代码

mobjSqlParameter.Value = strPayerBusinessName == null? strPayerBusinessName
:" Hello";

不会使用字符串Hello填充mobjSqlParameter.Value。当
strPayerBusinessName为null时。

v2中是否已弃用此语法?
In v1.x, the following code worked perfectly:

mobjSqlParameter.Value = strPayerBusinessName == null ? strPayerBusinessName
: (object)DBNull.Value;

However, I can''t get it to work in v2. I know that the value of
strPayerBusinessName is null because if I write

strPayerBusinessName == null in the Immediate Window, it returns "true".

However, the expression does not seem to evaluate the right-hand side, no
matter what I write. E.g., the following code

mobjSqlParameter.Value = strPayerBusinessName == null ? strPayerBusinessName
: "Hello";

does not populate mobjSqlParameter.Value with the string "Hello" when
strPayerBusinessName is null.

Has this syntax been deprecated in v2?




否。我怀疑其他内容正在发生。


你能发一个简短但完整的程序来演示

的问题吗?


http://www.pobox.com/~skeet/csharp/complete.html 有关详细信息

我的意思是什么。


您可能对null合并运算符感兴趣,但是,

会让你这样做:


mobjSqlParameter.Value = strPayerBusinessName ?? 你好;


http://www.pobox.com/~skeet/csharp/c.../nullable.html 了解更多

详情。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

如果回复该群组,请不要给我发邮件



No. I suspect something else is going on.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

You might be interested in the null coalescing operator, however, which
would let you do:

mobjSqlParameter.Value = strPayerBusinessName ?? "Hello";

See http://www.pobox.com/~skeet/csharp/c.../nullable.html for more
details.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


" Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
您可能对null合并运算符感兴趣,但是,它可以让您这样做:

mobjSqlParameter.Value = strPayerBusinessName ?? "你好英寸;


完美!非常感谢,Jon一如既往!

参见 http://www.pobox.com/~skeet/csharp/c.../nullable.html 了解更多详情。
You might be interested in the null coalescing operator, however, which
would let you do:

mobjSqlParameter.Value = strPayerBusinessName ?? "Hello";
Perfect! Thanks a lot, Jon, as always!
See http://www.pobox.com/~skeet/csharp/c.../nullable.html for more
details.




非常有用!



Incredibly useful!


这篇关于?和:用于v2中的条件表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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