954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Clones... Deep? Shallow??

Hello :)
well... i need to do a simple thing (i guess) but it seems that this is not so simple due to all the complicating examples on the web...

ok. i jast wanna to create a copy of an object! but i want the copy to be seperate from the original, so when i change something in the copy it will not affect the original. in this case i need Shallow-Copy or Deep-Copy? i thought its a Deep-Copy until i saw an example that confused me...

now - how i implement it?

lets say i have this Class -

public class Position
    {
        private bool _Turn;
        private AAAA _EN;


        public bool Turn
        {
            get { return _Turn; }
            set { _Turn = value; }
        }
        public AAAA EN
        {
            get { return _EN; }
            set { _EN = value; }
        }


this is when AAAA is a class too

well, i understand that i needed to add IClonable and Clone() Methode like that:

public class Position: IClonable
    {
        private bool _Turn;
        private AAAA _EN;


        public bool Turn
        {
            get { return _Turn; }
            set { _Turn = value; }
        }
        public AAAA EN
        {
            get { return _EN; }
            set { _EN = value; }
        }

        public Position Clone()
       {
            what in here??? i got confuse...
       }


Hope u understand my question...
Thanks in advance

galhajaj
Light Poster
36 posts since May 2011
Reputation Points: 13
Solved Threads: 0
 

Asked and answered over here , and it's a deep copy you want :)

Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

Thank u Monarch, but i dont understand the code in there... there are a lot of methods i dont know and i still do not use Generics yet so its not clear to me...

could you please give an example of implementing of this code?
the class ObjectCopier is the class i need to copy or its a class that just help me do that on another class?

Thank u

galhajaj
Light Poster
36 posts since May 2011
Reputation Points: 13
Solved Threads: 0
 

If you put that code into a file by itself, you'll be able to use it with any class that is serializable, doing this:

Position a = new Position();
Position b = a.Clone();
Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

If you put that code into a file by itself, you'll be able to use it with any class that is serializable, doing this:

Position a = new Position();
Position b = a.Clone();

Thank u - one last thing.. i have notice that i need to make the class "static" for that piece of code will work...
why is that? what will happen to my class after that? it can ruin things?

thanks.. :)

galhajaj
Light Poster
36 posts since May 2011
Reputation Points: 13
Solved Threads: 0
 

It needs to be static because it is a special type of method known as an 'extension method'. It 'extends' other classes rather than being used by the class it's in.

Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

Thanks :)

galhajaj
Light Poster
36 posts since May 2011
Reputation Points: 13
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: