GunnerFan420
GunnerFan420

Reputation: 198

Changes to New EntityType Variable Update Source Object

(Update: Added work-around after initial code block)

I'm not claiming anything is wrong, just don't understand how this works...

Using Visual Studio 2012 Express for Windows Desktop and Entity Framework 5

I've a new windows forms application with one form FrmHome which runs at app startup. I've a ConceptualEntityModel named GrowModel. I've one EntityType named Plant. Plant has two Properties Id and Name. Assume there are a few records in Plant.

The messagebox which is raised by the following code shows the same value ("XXX") for both currentPlant.Name and plantCopy.Name. I thought by using New when declaring plantCopy I'd get a separate copy of the Plant object and changes to plantCopy properties wouldn't affect currentPlant.

Can someone clue me as to how this works?

Thank you!

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

(Update 1)

Currently I'm using the code below as work around because you can copy elements of the Plant without passing a reference. Notice the added CopyPlant function and it's use when assigning a value to plantCopy. Assume two additional properties for Plant named Notes and LastUpdate. Using the below code the plantCopy.Name is 'XXX' and the currentPlant.Name is "OriginalValue". Still seems like you should be able to copy one plant to another without the reference but if I have to do the extra work then I will.

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

Upvotes: 1

Views: 83

Answers (1)

Rattle
Rattle

Reputation: 2553

You are overwriting the reference so that plantCopy and currentPlant both refer to the same object, replacing the seperate copy of Plant you created with New.

You can use http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx to clone the object rather than copy the reference, or search for many similar questions.

Upvotes: 1

Related Questions