Duplicate instance.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tiësto

    Duplicate instance.

    Hi everybody. I know everyone has said to me that this doesn't exist but I'm
    going to try anyway.

    I have an instance of ClassA and I want to duplicate that instance, creating
    another one that is independient of the first one. Isn't there any automatic
    way to do this?
    Do I have to write IClonable implementation for each and every class?

    Best Regards


  • Gabriel Lozano-Morán

    #2
    Re: Duplicate instance.

    System.Object has the method MemberwiseClone (). This will create a shallow
    copy and not a deep copy. This means that reference types will be shared
    across the copies. Value types in contrary will be copied.

    If you are familiar with the book "Design Patterns" written by the gang of
    four you should take a look at the Prototype pattern.

    Gabriel Lozano-Morán
    Software Engineer
    Sogeti

    "Tiësto" <a@a.com> wrote in message
    news:%23L67xQFR FHA.2748@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > Hi everybody. I know everyone has said to me that this doesn't exist but
    > I'm going to try anyway.
    >
    > I have an instance of ClassA and I want to duplicate that instance,
    > creating another one that is independient of the first one. Isn't there
    > any automatic way to do this?
    > Do I have to write IClonable implementation for each and every class?
    >
    > Best Regards
    >
    >[/color]


    Comment

    • Tiësto

      #3
      Re: Duplicate instance.

      Gracias Gabriel. I see this topic is a little complex, as I've seen many
      different positions about where/how long/why we should use IClonable
      interface or not. Some people say it will be deprecated on framework 2.0, as
      you never know if it returns a shallow copy or a deep copy. I thought there
      would be a method like MemberwiseClone (), with the diference that it could
      create a real copy of the reference types, creating new memory locations and
      copying the values from the first one.

      I see i have to implement that by myself.

      Thanks anyway!

      "Gabriel Lozano-Morán" <gabriel.lozano @no-spam.com> wrote in message
      news:ug4NSgFRFH A.3672@TK2MSFTN GP10.phx.gbl...[color=blue]
      > System.Object has the method MemberwiseClone (). This will create a shallow
      > copy and not a deep copy. This means that reference types will be shared
      > across the copies. Value types in contrary will be copied.
      >
      > If you are familiar with the book "Design Patterns" written by the gang of
      > four you should take a look at the Prototype pattern.
      >
      > Gabriel Lozano-Morán
      > Software Engineer
      > Sogeti
      >
      > "Tiësto" <a@a.com> wrote in message
      > news:%23L67xQFR FHA.2748@TK2MSF TNGP09.phx.gbl. ..[color=green]
      >> Hi everybody. I know everyone has said to me that this doesn't exist but
      >> I'm going to try anyway.
      >>
      >> I have an instance of ClassA and I want to duplicate that instance,
      >> creating another one that is independient of the first one. Isn't there
      >> any automatic way to do this?
      >> Do I have to write IClonable implementation for each and every class?
      >>
      >> Best Regards
      >>
      >>[/color]
      >
      >[/color]


      Comment

      • Daryush

        #4
        RE: Duplicate instance.

        One thing that you can do is to implement a copy constructor. Like:

        public ClassA( ClassA classA )
        {
        // Create the copy object here
        }

        And later in your program's actual processing part:
        ....
        ClassA originalClass = new ClassA();
        ....
        ClassA copyClass = new ClassA( originalClass );
        ....

        Daryush

        "Tiësto" wrote:
        [color=blue]
        > Hi everybody. I know everyone has said to me that this doesn't exist but I'm
        > going to try anyway.
        >
        > I have an instance of ClassA and I want to duplicate that instance, creating
        > another one that is independient of the first one. Isn't there any automatic
        > way to do this?
        > Do I have to write IClonable implementation for each and every class?
        >
        > Best Regards
        >
        >
        >[/color]

        Comment

        • Gabriel Lozano-Morán

          #5
          Re: Duplicate instance.

          I have taken a look at several classes in the .NET framework that implement
          the IClonable interface. What these actually do is create a new object
          instance using parameterized constructors. What you could try but I am not
          sure that this will work is mark your class with the [Serializable]
          attribute and then serialize it to a memory stream and then deserialize it.

          Gabriel Lozano-Morán
          Software Engineer
          Sogeti

          "Tiësto" <a@a.com> wrote in message
          news:eJrNgtFRFH A.3336@TK2MSFTN GP09.phx.gbl...[color=blue]
          > Gracias Gabriel. I see this topic is a little complex, as I've seen many
          > different positions about where/how long/why we should use IClonable
          > interface or not. Some people say it will be deprecated on framework 2.0,
          > as you never know if it returns a shallow copy or a deep copy. I thought
          > there would be a method like MemberwiseClone (), with the diference that it
          > could create a real copy of the reference types, creating new memory
          > locations and copying the values from the first one.
          >
          > I see i have to implement that by myself.
          >
          > Thanks anyway!
          >
          > "Gabriel Lozano-Morán" <gabriel.lozano @no-spam.com> wrote in message
          > news:ug4NSgFRFH A.3672@TK2MSFTN GP10.phx.gbl...[color=green]
          >> System.Object has the method MemberwiseClone (). This will create a
          >> shallow copy and not a deep copy. This means that reference types will be
          >> shared across the copies. Value types in contrary will be copied.
          >>
          >> If you are familiar with the book "Design Patterns" written by the gang
          >> of four you should take a look at the Prototype pattern.
          >>
          >> Gabriel Lozano-Morán
          >> Software Engineer
          >> Sogeti
          >>
          >> "Tiësto" <a@a.com> wrote in message
          >> news:%23L67xQFR FHA.2748@TK2MSF TNGP09.phx.gbl. ..[color=darkred]
          >>> Hi everybody. I know everyone has said to me that this doesn't exist but
          >>> I'm going to try anyway.
          >>>
          >>> I have an instance of ClassA and I want to duplicate that instance,
          >>> creating another one that is independient of the first one. Isn't there
          >>> any automatic way to do this?
          >>> Do I have to write IClonable implementation for each and every class?
          >>>
          >>> Best Regards
          >>>
          >>>[/color]
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • Tiësto

            #6
            Re: Duplicate instance.

            The main problem, I see, is to create "indepepend ent copies" of the
            reference types.

            I imagine a recursive method that, given an instance of a class, will create
            a copy of it and all of its value type values. But when it finds a reference
            type, it should run this method recursively to create a new copy, and so,
            and so...

            BUT. (and now I think I'm really understanding the purpose of ICloneable) we
            couldn't do this beacause not all classes have a default argument-less
            constructor. So, we couldn't know in advance HOW the instance is to be
            created. And as the "new" keyword is mandatory in order to allocate new
            memory for the new instance (and thus creating independent instances) .

            In an ideal escenario, all the classes would have a Clone method, which
            creates a shallow copy and calls Clone method of every reference type, or
            manages to do the same with strings, which are also ref types.

            I'm just making questions to myself. Feel free to post your comments.
            Regards...


            "Gabriel Lozano-Morán" <gabriel.lozano @no-spam.com> wrote in message
            news:%23FCSfAGR FHA.3560@TK2MSF TNGP14.phx.gbl. ..[color=blue]
            >I have taken a look at several classes in the .NET framework that implement
            >the IClonable interface. What these actually do is create a new object
            >instance using parameterized constructors. What you could try but I am not
            >sure that this will work is mark your class with the [Serializable]
            >attribute and then serialize it to a memory stream and then deserialize it.
            >
            > Gabriel Lozano-Morán
            > Software Engineer
            > Sogeti
            >
            > "Tiësto" <a@a.com> wrote in message
            > news:eJrNgtFRFH A.3336@TK2MSFTN GP09.phx.gbl...[color=green]
            >> Gracias Gabriel. I see this topic is a little complex, as I've seen many
            >> different positions about where/how long/why we should use IClonable
            >> interface or not. Some people say it will be deprecated on framework 2.0,
            >> as you never know if it returns a shallow copy or a deep copy. I thought
            >> there would be a method like MemberwiseClone (), with the diference that
            >> it could create a real copy of the reference types, creating new memory
            >> locations and copying the values from the first one.
            >>
            >> I see i have to implement that by myself.
            >>
            >> Thanks anyway!
            >>
            >> "Gabriel Lozano-Morán" <gabriel.lozano @no-spam.com> wrote in message
            >> news:ug4NSgFRFH A.3672@TK2MSFTN GP10.phx.gbl...[color=darkred]
            >>> System.Object has the method MemberwiseClone (). This will create a
            >>> shallow copy and not a deep copy. This means that reference types will
            >>> be shared across the copies. Value types in contrary will be copied.
            >>>
            >>> If you are familiar with the book "Design Patterns" written by the gang
            >>> of four you should take a look at the Prototype pattern.
            >>>
            >>> Gabriel Lozano-Morán
            >>> Software Engineer
            >>> Sogeti
            >>>
            >>> "Tiësto" <a@a.com> wrote in message
            >>> news:%23L67xQFR FHA.2748@TK2MSF TNGP09.phx.gbl. ..
            >>>> Hi everybody. I know everyone has said to me that this doesn't exist
            >>>> but I'm going to try anyway.
            >>>>
            >>>> I have an instance of ClassA and I want to duplicate that instance,
            >>>> creating another one that is independient of the first one. Isn't there
            >>>> any automatic way to do this?
            >>>> Do I have to write IClonable implementation for each and every class?
            >>>>
            >>>> Best Regards
            >>>>
            >>>>
            >>>
            >>>[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • Steve Walker

              #7
              Re: Duplicate instance.

              In message <#FCSfAGRFHA.35 60@TK2MSFTNGP14 .phx.gbl>, Gabriel Lozano-Morán
              <gabriel.lozano @no-spam.com> writes[color=blue]
              >I have taken a look at several classes in the .NET framework that implement
              >the IClonable interface. What these actually do is create a new object
              >instance using parameterized constructors. What you could try but I am not
              >sure that this will work is mark your class with the [Serializable]
              >attribute and then serialize it to a memory stream and then deserialize it.[/color]

              <Shameful confession> I've done that before now. It works. </>

              --
              Steve Walker

              Comment

              • Bruce Wood

                #8
                Re: Duplicate instance.

                The difficulty is one of semantics and domain meaning. I, too,
                struggled with this problem a while back and never came to a
                satisfactory conclusion.

                The only conclusion I did come to is that there is no "blanket
                solution" that fits all scenarios. For some objects in my problem
                domain, a shallow copy is exactly what I want. For others, a deep copy
                is what I want. For still others, they need something in between.

                For example, if an object holds a reference to an immutable reference
                type (that is, a class that has been designed so that once constructed
                it can't be changed), then there's no sense in copying it, since (apart
                from threading considerations) two objects holding a reference to the
                same object causes no surprises.

                On the other hand, making a shallow copy of a tree structure is just
                plain useless.

                Even for a single class, sometimes a shallow copy makes sense, and in
                other situations in my code I need a deep copy.

                I gave up.

                (By the way, Equals() suffers from the same malaise: when are two
                objects really equal? What does that mean? Same bugbear in a different
                disguise.)

                Comment

                Working...