在VB.Net中将属性作为参数传递。这种行为的正确性

时间:2015-12-18 17:31:48

标签: vb.net

如果我有一个属性对象:

Private Sub GridView1_RowUpdating(sender As Object, e As  GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Try
 Dim con As New SqlConnection(ConnectionString)
 Dim row As GridViewRow = DirectCast(GridView1.Rows(e.RowIndex), GridViewRow)
 Dim id As TextBox = DirectCast(row.FindControl("SMasterID"), TextBox) 'Int32.Parse(GridView1.DataKeys(e.RowIndex).Value.ToString())
 Dim tdate As TextBox = DirectCast(row.FindControl("ReviewDate"), TextBox)
 Dim tname As TextBox = DirectCast(row.FindControl("Reviewer"), TextBox)
 Dim cmd As New SqlCommand("update SMasterCurrentYear set Reviewer= '" + @reviewer + "',ReviewDate= " + @ReviewDate+ " where SMasterID =@id", con)
 GridView1.EditIndex = -1
 cmd.Parameters.Add("@id", SqlDbType.Int).Value = id
 cmd.Parameters.Add("@reviewer", SqlDbType.VarChar, 30).Value = tname.Text      cmd.Parameters.Add("@ReviewDate", SqlDbType.Date).Value = tdate.Text 
 con.Open()
 cmd.ExecuteNonQuery()
 con.Close()
 bind()
 Catch ex As Exception
   Throw New Exception(ex.Message)
 End Try
End Sub

我以这种方式将该属性作为参数传递给函数:

Public Class TestClass

    Public Property Prop1 As Integer
    Public Property Prop2 As Integer

    Public Sub New(PropValue As Integer)
        Prop1 = PropValue
        Prop2 = PropValue * 2
    End Sub

End Class

我做的测试:

Public Function TestFunctors(Obj As TestClass, PropertyFunc As Func(Of TestClass, Integer)) As Integer
    Return PropertyFunc(Obj)
End Function

一切正常,但我想知道我的第二次测试是否一致。我认为这意味着:Sub Main() Dim testObject = New TestClass(4) Console.WriteLine(String.Format("Must return 4: {0}, Must return 8: {1}", TestFunctors(testObject, Function() testObject.Prop1), TestFunctors(testObject, Function() testObject.Prop2))) Console.WriteLine(String.Format("Must return 77: {0}", TestFunctors(testObject, Function() 77))) Console.ReadKey() End Sub Obj.Prop1,但是当传递的函数是常量时,Obj.Prop2为什么不抛出?我确定这种行为总是一样的吗?有没有它不能工作的情况?

2 个答案:

答案 0 :(得分:1)

我不确定是不是这样。

检查一下:

Module StartupModule

    Sub Main()

        Dim testObject = New TestClass(4)

        Dim p1 As Func(Of TestClass, Integer) = Function(item)
                                                    Return item.Prop1
                                                End Function

        Dim p2 As Func(Of TestClass, Integer) = Function(item)
                                                    Return item.Prop2
                                                End Function

        Dim p3 As Func(Of TestClass, Integer) = Function(item)
                                                    Return 77
                                                End Function
        ' mind this
        Dim p4 As Func(Of TestClass, Integer) = Function() 77

        Console.WriteLine(String.Format("Must return 4: {0}, Must return 8: {1}", TestFunctors(testObject, p1), TestFunctors(testObject, p2)))
        Console.WriteLine(String.Format("Must return 77: {0}", TestFunctors(testObject, p4)))


        Console.ReadLine()
    End Sub

    Public Function TestFunctors(Obj As TestClass, PropertyFunc As Func(Of TestClass, Integer)) As Integer
        Return PropertyFunc(Obj)
    End Function


    Public Class TestClass

        Public Property Prop1 As Integer
        Public Property Prop2 As Integer

        Public Sub New(PropValue As Integer)
            Prop1 = PropValue
            Prop2 = PropValue * 2
        End Sub

    End Class


End Module

答案 1 :(得分:0)

我认为通过使用匿名函数返回属性会使事情过于复杂。请参阅酒店。

Dim testObject = New TestClass(4)
Console.WriteLine(testObject.Prop1, testObject.Prop2)