vb.net 4.0 iif问题有参数集时

时间:2011-07-14 15:18:27

标签: vb.net asp.net-4.0

谁能看到我在这里做错了什么?页面应该是“这是一个测试”

Partial Public Class testForm1
    Inherits System.Web.UI.Page
    Private Property test() As String
        Get
            'if is in session, return it, otherwise look it up
            If (IsNothing(Session("test"))) Then
                Session("test") = ""
            End If

            Return Session("test")

        End Get
        Set(ByVal Value As String)
            Session("test") = Value
        End Set
    End Property

    Public ReadOnly Property Istest As Boolean
        Get
            IIf(test.Contains("yes"), True, False)

        End Get
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        test = "yes"
        Response.Write(IIf(Istest, "YES This is a test", "NO testing here"))

    End Sub



End Class

1 个答案:

答案 0 :(得分:1)

你刚刚错过了Istest中的回归:

Public ReadOnly Property Istest As Boolean
    Get
        Return IIf(test.Contains("yes"), True, False)
    End Get
End Property

有关您的代码的两个提示。

  1. 使用If() operator代替Iif,它的工作方式相同但使用短路评估。我没有理由认为Iif()优于If()。
  2. 您实际上并不需要在IsTest属性中使用If或Iif:

  3. Public ReadOnly Property Istest As Boolean
        Get
            Return test.Contains("yes")
        End Get
    End Property