Copying existing graphic to another graphic layer - out of range exeception.

2935
5
01-06-2012 12:35 PM
Ravichandran_M_Kaushika
Occasional Contributor
dear Readers,

good afternoon.  I have successfully loaded graphics from SQL Server geometry objects using certain query conditions and associated them with certain colors based on their attribute values.

When user selects them from the grid (not esri feature grid - this app was begun at esri API 1.x), the selected row is associated with the graphic in the collection using an ID and it is flashed for a few seconds using utils.flash.  to make it convenient on the user, the 1st vertex is highlighted with a pushphin.  all these work great.

PROBLEM:
the users want to highlight the selected graphic in a different color - since cyan is not used in the coloring scheme for displaying attribute information, and since majority of the users come from an ArcMap background, i thought it might be appropriate to assign it to cyan.

so i copied the selected graphic into another graphic object and tried to add it to ANOTHER LAYER and the application is complaining that:
Specified argument was out of the range of valid values. Parameter name: Graphic is already associated with another layer.

@ esri.arcgis.client.graphicslayer.addgraphic(graphic g).

i have attached a pdf of the error message screen capture.  i did a exc.ToString() and got all the info I could.

i do not wnat to complicate the app by adding the Nth dictionary to keep track of the existing color and the ID - clearing it etc.  if it comes to that, i will do it.  i was wondering whether there was another simpler way of doing it.

thanks for your help.

regards
Ravi.
0 Kudos
5 Replies
ChristopherHill
Occasional Contributor
Hi Ravi,

When copying the graphic you need to use the Clone() method or you will have two references to the same graphic, when you try to add the second graphic reference to another GraphicsLayer our API will throw an exception because it knows that the graphic belongs to a different GraphicsLayer already. If you want to copy to a new GraphicsLayer you can make a new instance like below.

Graphic myGraphic = new Graphic();
myGraphic.Geometry = selectedGraphic.Geometry.Clone();

My question to you is why are you going to this length in the first place. If you are using FeatureLayer with graphics comming from a FeatureServer then a selection color is built into the FeatureLayer and all you have to do is set myGraphic.Selected = true and the renderer associated with the FeatureLayer will change the selection color for you. then when you set MyGraphic.Selected = false the color will change back.

If you are using a GraphicsLayer you will need to add selection state to your symbols using this SDK Sample
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SelectGraphics
0 Kudos
Ravichandran_M_Kaushika
Occasional Contributor
Chris,

good afternoon.  i did not find existingGraphics.Geometry.Clone() function in the geometry member.  So i will not be able implement your idea.

i do understand that featureservice does a lot for us these days.  thanks for the suggestion.  i am not able to implement due to project timeline considerations:

the app that I am trying to complete began on the ArcGIS SL API 1 Beta in March/Apr 2009 after a previous version on WebADF did not address user experience requirements.  it was slow and writing to sde was a pain.

then Silverlight api 1 beta showed better hope and we found that we had to write code to persist data in sde.  so i wrote modules as explained in http://forums.esri.com/Thread.asp?c=158&f=2455&t=280568#871904.

then writing to sde was a problem because i instantiated fine grained arcobjects on teh server and sometimes it will take soemtime to get loaded into memory and there were perf issues.  so i wrote the geometries as sql server geometry objects by Sep 2009.  the app then went into pilot phase in mid 2010 and this request is the last change i am trying to make before the app can go live nationwide.

any kind of technology change or implementation change has to go through many testing - particularly a total one like this.  it is time consuming to go back n 'fix' everything n test it.

due to functions not available in ESRI SL API in 1.x and in early parts 2, i read the geometry object into the client, then go thro the coords, construct the esri client geometres on the fly and based on a certain attribute status,  i set the color on the fly.

thanks for your input.  i am planning to use http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SpatialQuery- visual state manager to see whether i can make anything happen.  i had some hiccups and had to toss out the changes and re-checked (undo changes) the code from the source control to test out your suggestions.

appreciate your help
regards
Ravi.


Hi Ravi,

When copying the graphic you need to use the Clone() method or you will have two references to the same graphic, when you try to add the second graphic reference to another GraphicsLayer our API will throw an exception because it knows that the graphic belongs to a different GraphicsLayer already. If you want to copy to a new GraphicsLayer you can make a new instance like below.

Graphic myGraphic = new Graphic();
myGraphic.Geometry = selectedGraphic.Geometry.Clone();

My question to you is why are you going to this length in the first place. If you are using FeatureLayer with graphics comming from a FeatureServer then a selection color is built into the FeatureLayer and all you have to do is set myGraphic.Selected = true and the renderer associated with the FeatureLayer will change the selection color for you. then when you set MyGraphic.Selected = false the color will change back.

If you are using a GraphicsLayer you will need to add selection state to your symbols using this SDK Sample
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#SelectGraphics
0 Kudos
Ravichandran_M_Kaushika
Occasional Contributor
Chris,

i think the function is should be:

myNewGraphic.Geometry = ESRI.ArcGIS.Client.Geometry.Clone(gSelectedGraphic.Geometry);

i used it and i was able to see the graphics on the screen.

thank you for your help.

regards
Ravi.
0 Kudos
ChristopherHill
Occasional Contributor
Ah, your using our Silverlight API v1.x, that makes a big difference. If you ever get the chance to upgrade to our current version a lot of what you are trying to do would be a lot easier to accomplish. Glad you got it all worked out.
0 Kudos
Ravichandran_M_Kaushika
Occasional Contributor
Chris,

thanks for your time.  we have upgraded the underlying API dll to 2.x and re-compiled, but could not make use of all the 'special/new' features of 2.x if it involved a lot of re-writing sections of the app.  for example, we used the webmercator converter object because i did not have disturb the existing structure of the app - but cannot use the SDE writing/update functions from feature server REST service to edit sde (reg and editable) layer.

thanks for your patience and help.

regards
Ravi.

Ah, your using our Silverlight API v1.x, that makes a big difference. If you ever get the chance to upgrade to our current version a lot of what you are trying to do would be a lot easier to accomplish. Glad you got it all worked out.
0 Kudos