1

(更新:在初始代码块之后添加了解决方法)

我没有声称有什么问题,只是不明白这是如何工作的......

使用适用于 Windows 桌面和实体框架 5 的 Visual Studio 2012 Express

我有一个新的 Windows 窗体应用程序,其中一个窗体 FrmHome 在应用程序启动时运行。我有一个名为 GrowModel 的概念实体模型。我有一个名为 Plant 的 EntityType。Plant 有两个属性 ID 和名称。假设 Plant 中有一些记录。

以下代码引发的消息框显示 currentPlant.Name 和 plantCopy.Name 的相同值(“XXX”)。我认为通过在声明plantCopy 时使用New 可以获得Plant 对象的单独副本,并且对plantCopy 属性的更改不会影响currentPlant。

有人可以告诉我这是如何工作的吗?

谢谢!

Public Class FrmHome

    Private Shared m_growData As New GrowEntities

    Public Sub DoSomething() Handles Me.Load

        Dim currentPlant As Plant
        currentPlant = FindPlantById(1)

        Dim plantCopy as New Plant
        plantCopy = currentPlant

        plantCopy.Name = "XXX"

            MessageBox.Show("Current Plant Name: " & _
            currentPlant.Name & ControlChars.CrLf & _
            "Plant Copy Name:" & plantCopy.Name)

    End Sub



    Shared Function FindPlantById(ByVal PlantId As Integer) As Plant

        Dim PlantList As New List(Of Plant)
        PlantList = m_growData.Plants.ToList

        Dim intPlantLoopCount As Integer = PlantList.Count - 1

        For y = 0 To intPlantLoopCount

            Dim currentPlant As Plant = PlantList(y)

            If currentPlant.Id = PlantId Then
                Return currentPlant
            End If

        Next

        Return Nothing

    End Function

End Class

(更新 1)

目前我正在使用下面的代码作为解决方法,因为您可以在不传递引用的情况下复制 Plant 的元素。注意添加的 CopyPlant 函数,它在为 plantCopy 赋值时使用。假设 Plant 有两个附加属性,名为 Notes 和 LastUpdate。使用下面的代码,plantCopy.Name 是“XXX”,currentPlant.Name 是“OriginalValue”。似乎您仍然应该能够在没有参考的情况下将一种植物复制到另一种植物,但如果我必须做额外的工作,那么我会的。

Public Class FrmHome

    Private Shared m_growData As New GrowEntities

    Public Sub DoSomething() Handles Me.Load

        Dim currentPlant As Plant
        currentPlant = FindPlantById(1)

        Dim plantCopy as New Plant
        plantCopy = CopyPlant(currentPlant)

        plantCopy.Name = "XXX"

            MessageBox.Show("Current Plant Name: " & _
            currentPlant.Name & ControlChars.CrLf & _
            "Plant Copy Name:" & plantCopy.Name)

    End Sub


    Shared Function FindPlantById(ByVal PlantId As Integer) As Plant

        Dim PlantList As New List(Of Plant)
        PlantList = m_growData.Plants.ToList

        Dim intPlantLoopCount As Integer = PlantList.Count - 1

        For y = 0 To intPlantLoopCount

            Dim currentPlant As Plant = PlantList(y)

            If currentPlant.Id = PlantId Then
                Return currentPlant
            End If

        Next

        Return Nothing

    End Function

    Shared Function CopyPlant(ByVal source As Plant) As Plant

        Dim target As New Plant

        target.Name = source.Name
        target.Notes = source.Notes
        target.LastUpdate = Now()

        Return target

    End Function

End Class
4

1 回答 1

1

您正在覆盖引用,因此 plantCopy 和 currentPlant 都引用同一个对象,将您创建的 Plant 的单独副本替换为 New。

您可以使用http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx来克隆对象而不是复制引用,或者搜索许多类似的问题。

于 2013-06-23T22:23:05.303 回答