How can I clone a graphic object?

2762
4
05-15-2011 08:58 AM
weiliang
New Contributor II
Hi,

I want to clone a graphic object but find that it doesn't implement the ICloneable interface. Then how can I fulfill this graphic clone purpose? Will ESRI plans to make the graphic cloneable in next release?

Thanks,

Wei
0 Kudos
4 Replies
DominiqueBroux
Esri Frequent Contributor
I am not aware to any plan to implement the ICloneable interface.

For a solution, look at this thread : http://forums.arcgis.com/threads/30252-How-to-create-a-new-graphic-similar-to-a-graphic-in-some-feat...
0 Kudos
weiliang
New Contributor II
Many thanks, Dominique. But I thought geometry is reference type, why it is not and don't need to be deep cloned? I'm a little bit confused.
           
   For example the code below, why after the last line is been executed, the gra.Geometry is not null:
            MapPoint mapPoint = new MapPoint(2.4, 42.6);
            Graphic gra = new Graphic();           
            gra.Geometry = mapPoint;
            mapPoint = null;

Thank again,

Wei
0 Kudos
DominiqueBroux
Esri Frequent Contributor

Many thanks, Dominique. But I thought geometry is reference type, why it is not and don't need to be deep cloned?


Hi Wei,

You may have to clone the geometry depending on what you are doing with your graphics. If you want to be able to move a graphic without moving its clone, you have to deep clone the geometry.
Luckily the geometry class supports a 'Clone' method. So easy to do:)

For example the code below, why after the last line is been executed, the gra.Geometry is not null:
MapPoint mapPoint = new MapPoint(2.4, 42.6);
Graphic gra = new Graphic();
gra.Geometry = mapPoint;
mapPoint = null;


'mapPoint = null;' changes the object the variable is referencing to but doesn't change the object itself. So gra.geometry is not impacted.

However, if you set 'mapPoint.X=0;mapPoint.Y=0;', you would change the geometry of your graphic since mapPoint and gra.Geometry are referencing the same object.
0 Kudos
weiliang
New Contributor II
Thanks, Dominique. It's good to know that set the variable to null isn't really clear the heap object.

Wei
0 Kudos