locked
why the data source updated as well when the variable is updated RRS feed

  • Question

  • Hi, part of my code is in below

    path =

    New NextGenList

    path.Path =

    New Dictionary(Of Integer, Integer)

     

    If tempnextgen.Item(m).GenNum = branchlist.Item(j).FromBus Then

     

    'add ToBus into the path list

    path.GenNum = branchlist.Item(j).ToBus

    path.Distance = branchlist.Item(j).Length + tempnextgen.Item(m).Distance

    path.Path = tempnextgen.Item(m).Path

    path.Path.Add(tempnextgen.Item(m).Path.Count, branchlist.Item(j).ToBus)

    newnextgenlist.Add(path)

    selectbranchlist.Add(j)

    'record the selected branch

     

    End If

    My question is when I perform the

    path.Path.Add(tempnextgen.Item(m).Path.Count, branchlist.Item(j).ToBus)

    I would expect to see the path.Path include the new record, however, I also notice that tempnextgen also include the new record. Can anyone help me out on this issue? Why the tempnextgen got updated as well, how to prevent this unexpected operation? Thanks.


    Fred
    Wednesday, July 1, 2009 2:02 AM

Answers

  • Huang,

    As I wrote already, try to get your datanames right, I tried to create from the bunch of code something readable.

    I show it here using that </> box where I selected VB Net in.

    When I look to it, then suddenly from the air comes something as tempnextgen (in that area should your problem come)

    Public Class Form1
        Private Sub Whatever()
            Dim tempgenlist = New NextGenList
            tempgenlist.Corridor = New Dictionary(Of Integer, Integer)
            'try to copy some previous found corridor into this tempgenlist
            tempgenlist.Corridor = tempnextgen.Item(m).corridor
            'try to add one more element into this tempgenlist.corridor
            tempgenlist.Corridor.Add(tempnextgen.Item(m).corridor.Count, branchlist.Item(j).ToBus)
        End Sub
    End Class
    Public Class NextGenList
        Private _genNum As Integer
        Public Property GenNum() As Integer
            Get
                Return _genNum
            End Get
            Set(ByVal value As Integer)
                _genNum = value
            End Set
        End Property
        Private _distance As Double
        Public Property Distance() As Double
            Get
                Return _distance
            End Get
            Set(ByVal value As Double)
                _distance = value
            End Set
        End Property
        Private _corridor As Dictionary(Of Integer, Integer)
        Public Property Corridor() As Dictionary(Of Integer, Integer)
            Get
                Return _corridor
            End Get
            Set(ByVal value As Dictionary(Of Integer, Integer))
                _corridor = value
            End Set
        End Property
    End Class
    


    Success
    Cor
    • Marked as answer by Xingwei Hu Tuesday, July 7, 2009 7:02 AM
    Wednesday, July 1, 2009 2:41 PM

All replies

  • Fred,

    I would first start to change your names Path in something else, your code is confusing now and hardey to understand therefore hard to maintain and therefore hard to be helped by others.


    http://msdn.microsoft.com/en-us/library/system.io.path.aspx

    Success
    Cor
    Wednesday, July 1, 2009 5:38 AM
  • Sorry about the name confusion. Let me try to reinstate the issue I have in the program.

    A class is defined

    Public

     

    Class NextGenList

     

    Private _genNum As Integer

     

    Public Property GenNum() As Integer

     

    Get

     

    Return _genNum

     

    End Get

     

    Set(ByVal value As Integer)

    _genNum = value

     

    End Set

     

    End Property

     

    Private _distance As Double

     

    Public Property Distance() As Double

     

    Get

     

    Return _distance

     

    End Get

     

    Set(ByVal value As Double)

    _distance = value

     

    End Set

     

    End Property

     

    Private _corridor As Dictionary(Of Integer, Integer)

     

    Public Property Corridor() As Dictionary(Of Integer, Integer)

     

    Get

     

    Return _corridor

     

    End Get

     

    Set(ByVal value As Dictionary(Of Integer, Integer))

    _corridor = value

     

    End Set

     

    End Property

    End

     

    Class


    Then inside the program


    tempgenlist =

    New NextGenList

    tempgenlist.corridor =

    New Dictionary(Of Integer, Integer)

    'try to copy some previous found corridor into this tempgenlist
    tempgenlist.corridor = tempnextgen.Item(m).corridor

    'try to add one more element into this tempgenlist.corridor
    tempgenlist.corridor.Add(tempnextgen.Item(m).corridor.Count, branchlist.Item(j).ToBus)

    My issue is after tempgenlist.corridor.add is performed, not only tempgenlist.corridor but also the tempnextgen.item(m).corridor are adding one element as well. How can I prevent this original tempnextgen.Item(m).corridor from adding one element when I try to add one element into the tempgenlist.corridor? Hope this is clear for you to help me on this issue.

    Best regards,


    Fred
    Wednesday, July 1, 2009 1:49 PM
  • Huang,

    As I wrote already, try to get your datanames right, I tried to create from the bunch of code something readable.

    I show it here using that </> box where I selected VB Net in.

    When I look to it, then suddenly from the air comes something as tempnextgen (in that area should your problem come)

    Public Class Form1
        Private Sub Whatever()
            Dim tempgenlist = New NextGenList
            tempgenlist.Corridor = New Dictionary(Of Integer, Integer)
            'try to copy some previous found corridor into this tempgenlist
            tempgenlist.Corridor = tempnextgen.Item(m).corridor
            'try to add one more element into this tempgenlist.corridor
            tempgenlist.Corridor.Add(tempnextgen.Item(m).corridor.Count, branchlist.Item(j).ToBus)
        End Sub
    End Class
    Public Class NextGenList
        Private _genNum As Integer
        Public Property GenNum() As Integer
            Get
                Return _genNum
            End Get
            Set(ByVal value As Integer)
                _genNum = value
            End Set
        End Property
        Private _distance As Double
        Public Property Distance() As Double
            Get
                Return _distance
            End Get
            Set(ByVal value As Double)
                _distance = value
            End Set
        End Property
        Private _corridor As Dictionary(Of Integer, Integer)
        Public Property Corridor() As Dictionary(Of Integer, Integer)
            Get
                Return _corridor
            End Get
            Set(ByVal value As Dictionary(Of Integer, Integer))
                _corridor = value
            End Set
        End Property
    End Class
    


    Success
    Cor
    • Marked as answer by Xingwei Hu Tuesday, July 7, 2009 7:02 AM
    Wednesday, July 1, 2009 2:41 PM