2011-07-14 13 views
1

任何人都可以看到我在這裏做錯了嗎?該頁面應該「YES這是一個測試」vb.net 4.0 iif問題,當有一個參數集

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

你只是缺少Istest退貨:

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

兩個小技巧。

  1. 使用If() operator而不是Iif,其工作方式相同,但使用短路評估。沒有理由可以考慮通過If()來使用Iif()。
  2. 你實際上並不需要使用如果或IIF都在你的IsTest屬性:

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

+1,但更重要的,只是:'返回test.Contains( 「是」 )'不需要IIf()。 –

+0

好點!感謝那 :) – kiev