Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Serializing the same object instance twice...

Serializing the same object instance twice...

Scheduled Pinned Locked Moved C#
tutorialquestion
4 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shy Agam
    wrote on last edited by
    #1

    Hello, I have a collection of ITask (an interface I've created) objects. References to these objets are duplicated. They are kept in a second collection. This second collection is a collection of categories. Each category can contain child categories. Each task can appear only in one category. This means that every single ITask object in the ITask collection is somewhere inside the categories. I'm using this implementation to allow the user to filter the ITask objects by categories, and by other means which are not relevant. OK... Now I want the tasks to be "restored" when the program next runs, so I serialize and deserialize them... So far so good... I want to serialize the collection of categories the user have created, and restore them too... I can serialize the categories collection as well, but what will happen to the ITask objects when I deserialize the categories? Will they point to the same objects deserialized to the ITask collection? Hope this is clear enough... :) If you have any other ideas regarding how to better store the collections, I'd be more than happy to hear them. :) Thanks in advance, Shy.

    M W S 3 Replies Last reply
    0
    • S Shy Agam

      Hello, I have a collection of ITask (an interface I've created) objects. References to these objets are duplicated. They are kept in a second collection. This second collection is a collection of categories. Each category can contain child categories. Each task can appear only in one category. This means that every single ITask object in the ITask collection is somewhere inside the categories. I'm using this implementation to allow the user to filter the ITask objects by categories, and by other means which are not relevant. OK... Now I want the tasks to be "restored" when the program next runs, so I serialize and deserialize them... So far so good... I want to serialize the collection of categories the user have created, and restore them too... I can serialize the categories collection as well, but what will happen to the ITask objects when I deserialize the categories? Will they point to the same objects deserialized to the ITask collection? Hope this is clear enough... :) If you have any other ideas regarding how to better store the collections, I'd be more than happy to hear them. :) Thanks in advance, Shy.

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello,

      shyagam wrote:

      Will they point to the same objects deserialized to the ITask collection?

      No, I don't think so. You will then have the double number of ITask objects! I would store one Collection and additionaly the way the second collection was sorted. After deserialization you fill the second Collection with the ITask objects of the first, and Sort it again! All the best, Martin -- modified at 6:39 Wednesday 20th June, 2007 The advantage of storing the way you are sorting (with an enum for ex.), is that you are allways consistend with updated programs in combination with old datas.

      1 Reply Last reply
      0
      • S Shy Agam

        Hello, I have a collection of ITask (an interface I've created) objects. References to these objets are duplicated. They are kept in a second collection. This second collection is a collection of categories. Each category can contain child categories. Each task can appear only in one category. This means that every single ITask object in the ITask collection is somewhere inside the categories. I'm using this implementation to allow the user to filter the ITask objects by categories, and by other means which are not relevant. OK... Now I want the tasks to be "restored" when the program next runs, so I serialize and deserialize them... So far so good... I want to serialize the collection of categories the user have created, and restore them too... I can serialize the categories collection as well, but what will happen to the ITask objects when I deserialize the categories? Will they point to the same objects deserialized to the ITask collection? Hope this is clear enough... :) If you have any other ideas regarding how to better store the collections, I'd be more than happy to hear them. :) Thanks in advance, Shy.

        W Offline
        W Offline
        welbers
        wrote on last edited by
        #3

        Hello, if you serialize the collections using 2 Serialize() calls, you would end up with duplicates of each ITask object. I would do something like this (in C#):

        [Serializable]
        public class DataStore
        {
        public ICollection Tasks;
        public ICollection Categories;
        }

        Serializing this class will keep the references correct and will not produce duplicates. greetings, Enno Welbers

        1 Reply Last reply
        0
        • S Shy Agam

          Hello, I have a collection of ITask (an interface I've created) objects. References to these objets are duplicated. They are kept in a second collection. This second collection is a collection of categories. Each category can contain child categories. Each task can appear only in one category. This means that every single ITask object in the ITask collection is somewhere inside the categories. I'm using this implementation to allow the user to filter the ITask objects by categories, and by other means which are not relevant. OK... Now I want the tasks to be "restored" when the program next runs, so I serialize and deserialize them... So far so good... I want to serialize the collection of categories the user have created, and restore them too... I can serialize the categories collection as well, but what will happen to the ITask objects when I deserialize the categories? Will they point to the same objects deserialized to the ITask collection? Hope this is clear enough... :) If you have any other ideas regarding how to better store the collections, I'd be more than happy to hear them. :) Thanks in advance, Shy.

          S Offline
          S Offline
          Shy Agam
          wrote on last edited by
          #4

          Thank you both. Very helpful indeed... :) Shy.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups