使用内嵌IF语句vb.net

使用内嵌IF语句vb.net,vb.net,if-statement,Vb.net,If Statement,有关代码的简要信息如下。该代码采用一组字符串,并将它们与如下内容进行比较,中间的IF语句决定是否在其中之一上进行选择。问题是If(Evaluation,“,”)抱怨它不能为null或必须是资源。。当计算只是检查一个对象以确保它不是空的,并且检查对象中的一个属性时,如何解决这个问题,如下所示: Dim R as string = stringA & " * sample text" & _ stringB & " * sample text2" & _

有关代码的简要信息如下。该代码采用一组字符串,并将它们与如下内容进行比较,中间的IF语句决定是否在其中之一上进行选择。问题是
If(Evaluation,“,”)
抱怨它不能为null或必须是资源。。当计算只是检查一个对象以确保它不是空的,并且检查对象中的一个属性时,如何解决这个问题,如下所示:

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拒绝了它:

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三元表达式计算器

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(expression,truepart,falsepart)
这甚至更好,因为它提供了短路功能

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"

非常感谢,我想我应该去阅读这个IIF声明,因为我已经在其他帖子中看到了它…@Steve我已经尝试了上面的方法,看起来不错,但是如果你看看我的编辑,你就会看到它在说什么。。应该注意的是,我知道在我的示例中,我没有给y赋值。在实际使用的代码中,y被赋值或保持为0.0,有什么想法吗???此外,应该注意的是,我的OP与您发布的内容完全相同,问题甚至没有被看。。。为什么我在表达式上得到了一个VS标志???很抱歉最近的评论,但y是一个值类型(不可为null),vb编译器抱怨IsNot用于值类型,而在使用ApplyValue之类的引用类型时则无话可说。