更新 1 个对象似乎也会更新另一个对象。 VB网络
我的网站遇到问题,其中有两个对象在会话中,两个对象的结构不同,并且彼此之间没有任何链接(不是故意的)。然而,这两个对象都有“旅行者”的集合。我想做的是通过添加或删除“旅行者”来修改一个集合,当单击完成按钮时,修改后的集合将保存到另一个对象。
问题是,由于某种原因,当我修改一个对象中的一个集合时,也会对另一个对象中的集合进行修改。
这是一个粗略的示例:
Dim bookingContext As BookingContext = Session("BookingContext")
Dim personSearchContext As PersonSearchContext = Session("PersonSearchContext")
Dim personId As Integer = Request.Form("PersonID")
Dim mode As String = Request.Form("Mode")
Select Case mode
Case "Add"
For Each traveller As PersonProfile In personSearchContext.Travellers
If traveller.PersonID = personId Then
personSearchContext.SelectedTravellers.Add(traveller)
Exit For
End If
Next
context.Session("PersonSearchContext") = personSearchContext
Case "Remove"
For Each traveller As PersonProfile In personSearchContext.SelectedTravellers
If traveller.PersonID = personId Then
travellerSearchContext.SelectedTravellers.Remove(traveller)
Exit For
End If
Next
context.Session("PersonSearchContext") = personSearchContext
Case "Save"
bookingContext.Travellers = personSearchContext.SelectedTravellers
context.Session("BookingContext") = bookingContext
End Select
问题是当我在“PersonSearchContext”内的集合中添加和删除时,对象“BookingContext”内的 Travelers 集合正在更新。就好像它们之间存在某种链接或指针。
有什么想法吗?
干杯。
I having a problem in my website where I have 2 objects held in session, both object are structured differently and do not have any links to each other (not intentionally). Both objects however, have a collection of "Travellers". What I am trying to do is modify one collection by adding or removing "Travellers", when a finish button is clicked, the modified collection will then be saved to the other object.
The problem is that for some reason when I modify one collection in one object, the modifications are also made to the collection in the other object.
Here's a rough example:
Dim bookingContext As BookingContext = Session("BookingContext")
Dim personSearchContext As PersonSearchContext = Session("PersonSearchContext")
Dim personId As Integer = Request.Form("PersonID")
Dim mode As String = Request.Form("Mode")
Select Case mode
Case "Add"
For Each traveller As PersonProfile In personSearchContext.Travellers
If traveller.PersonID = personId Then
personSearchContext.SelectedTravellers.Add(traveller)
Exit For
End If
Next
context.Session("PersonSearchContext") = personSearchContext
Case "Remove"
For Each traveller As PersonProfile In personSearchContext.SelectedTravellers
If traveller.PersonID = personId Then
travellerSearchContext.SelectedTravellers.Remove(traveller)
Exit For
End If
Next
context.Session("PersonSearchContext") = personSearchContext
Case "Save"
bookingContext.Travellers = personSearchContext.SelectedTravellers
context.Session("BookingContext") = bookingContext
End Select
The issue is the Travellers collection within the object "BookingContext" is being updated when I add and remove from the collection within "PersonSearchContext". It's as though there is some sort of link or pointer between them.
Any ideas?
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于您的 Save 代码中的这一行:
您正在执行的操作是获取对 personSearchContext 对象中的 SelectedTravellers 集合的引用,并将该引用分配给 bookingContext 对象中的 Travelers 集合。此分配意味着 bookingContext.Travellers 不再是与 personSearchContext.SelectedTravellers 分开的集合。现在它们都是对内存中同一对象的引用。
The problem is this line in your Save code:
What you're doing there is taking the reference to the SelectedTravellers collection in the personSearchContext object and assigning the reference to the Travellers collection in the bookingContext object. This assignment means that bookingContext.Travellers is no longer a separate collection from personSearchContext.SelectedTravellers. They're now both references to the same object in memory.