pointers - Updating 1object appears to also update another. VB.Net -


i having problem in website have 2 objects held in session, both object structured differently , not have links each other (not intentionally). both objects however, have collection of "travellers". trying modify 1 collection adding or removing "travellers", when finish button clicked, modified collection saved other object.

the problem reason when modify 1 collection in 1 object, modifications made collection in other object.

here's rough example:

dim bookingcontext bookingcontext = session("bookingcontext") dim personsearchcontext personsearchcontext = session("personsearchcontext") dim personid integer = request.form("personid") dim mode string = request.form("mode")  select case mode     case "add"         each traveller personprofile in personsearchcontext.travellers             if traveller.personid = personid                 personsearchcontext.selectedtravellers.add(traveller)                 exit             end if         next         context.session("personsearchcontext") = personsearchcontext     case "remove"         each traveller personprofile in personsearchcontext.selectedtravellers             if traveller.personid = personid                 travellersearchcontext.selectedtravellers.remove(traveller)                 exit             end if         next         context.session("personsearchcontext") = personsearchcontext     case "save"          bookingcontext.travellers = personsearchcontext.selectedtravellers          context.session("bookingcontext") = bookingcontext                  end select 

the issue travellers collection within object "bookingcontext" being updated when add , remove collection within "personsearchcontext". it's though there sort of link or pointer between them.

any ideas?

cheers.

the problem line in save code:

bookingcontext.travellers = personsearchcontext.selectedtravellers 

what you're doing there taking reference selectedtravellers collection in personsearchcontext object , assigning reference travellers collection in bookingcontext object. assignment means bookingcontext.travellers no longer separate collection personsearchcontext.selectedtravellers. they're both references same object in memory.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -