【问题标题】:vb.net 4.0 iif issue when there is a parameter set有参数集时的vb.net 4.0 iif问题
【发布时间】:2011-07-14 15:18:26
【问题描述】:

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

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

【问题讨论】:

    标签: vb.net asp.net-4.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:

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

    【讨论】:

    • +1,但更好的是:Return test.Contains("yes") 不需要 IIf()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多