使用内联IF语句vb.net [英] Using inline IF statement vb.net

查看:184
本文介绍了使用内联IF语句vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关代码的简要信息如下。代码采用一堆字符串并将它们如下所示,在中间使用if语句决定是否在其中一个上是concant。问题是如果(评估,,)抱怨说它不能为空或者必须是资源..我该如何解决这个问题呢?评估只是检查一个对象以确保它IsNot Nothing以及对象中的属性被检查如下:

Brief info on the code is as follows. The code takes a bunch of strings and concants them as follows with a if statement in the middle that decides whether to concant or not on one of them. The problem is the If(Evaluation, "", "") is complaining saying that it must not be nullable or must be a resource.. How do I work around this when the Evaluation simply checks an object to make sure it IsNot Nothing and also that a property in the object is checked as follows:

Dim R as string = stringA & " * sample text" & _
    stringB & " * sample text2" & _
    stringC & " * sameple text3" & _
    If(ApplyValue IsNot Nothing AndAlso ApplyValue.CheckedBox Then ,StringD & " * sample text4" & _
    , NOTHING)
stringE & " * sample text5"

VS正在抱怨applyValue。任何想法?

VS is complaining about the applyValue. Any Ideas?

应该注意的是,我已经尝试过以下只是为了看看它是否有用而VS拒绝它:

Should be noted that I have tried the following just to see if it would work and VS is rejecting it:

Dim y As Double
Dim d As String = "string1 *" & _
    "string2 *" & _
    If(y IsNot Nothing, " * sample text4", "") & _
    "string4 *"

这是标记y的原因:

  'IsNot' requires operands that have reference types, but this operand has the value type 'Double'.    C:\Users\Skindeep\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 13  16  WindowsApplication1


推荐答案

使用IIF三元表达式评估器

Use the IIF ternary expression evaluator

Dim R as string = stringA & " * sample text" & _
                  stringB & " * sample text2" & _
                  stringC & " * sameple text3" & _
                  IIf(ApplyValue IsNot Nothing AndAlso ApplyValue.CheckedBox, StringD & " * sample text4", "") & _
                  stringE & " * sample text5"

编辑:如果您使用2008年以后的VB.NET,您也可以使用

If you use VB.NET from ver 2008 onward you could use also the

IF(expression,truepart,falsepart)

这更好,因为它提供了短路功能。

and this is even better because it provides the short-circuit functionality.

Dim R as string = stringA & " * sample text" & _
                  stringB & " * sample text2" & _
                  stringC & " * sameple text3" & _
                  If(ApplyValue IsNot Nothing AndAlso ApplyValue.CheckedBox, StringD & " * sample text4", "") & _
                  stringE & " * sample text5"

这篇关于使用内联IF语句vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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