Refering the layers by name

566
2
11-25-2010 05:59 PM
shravanshravan
New Contributor
In my TOC, i have four feature classes with the following names "watershed", "temperature", "watchtowers" and "precipitation". Here, we want the "watershed"  which is a polygon feature class and "watchtowers" which is a point feature class to be used for the analysis.

Could you please tell me, how can i programmatically refer the above two layers by "name", so that i can use the "watershed" and "watchtower" layer for the analysis (using C#)
0 Kudos
2 Replies
RuchiraWelikala
Occasional Contributor
You have to loop through the table of contents.  You do this with a For loop:

private void LayerName()
{
 IMxDocument pMxDoc = default(IMxDocument);
 IMap pMap = default(IMap);
 IFeatureLayer pFeatureLayer = default(IFeatureLayer);
 //Assign the mxDocument here..however you retrieved it...I usually use the snippet for retrieving the MxDoc from ArcMap
 pMxDoc = ThisDocument;
 pMap = pMxDoc.FocusMap;
 // Loop through each layer, and report its name.
 for (i = 1; i <= pMap.LayerCount; i++) {
  pFeatureLayer = pMap.Layer(i);
  Interaction.MsgBox("The name of layer is: " + pFeatureLayer.name);
 }
}


SO you match the name you want with teh one that is returned in the loop instance and go from there..hope this helps.
0 Kudos
shravanshravan
New Contributor
Thanks a lot for helping me.
0 Kudos