I'm trying to get a message box to display some simple counts of layers and fields. Everything works fine until I add in the code to get the field count of composite layers. Below is the code for my button click event which populates the message box, and the error message I am receiving.
private void buttonCountLayers_Click(object sender, EventArgs e)
{
//count the point/line/poly/raster feature layers
int groupcount = 0;
int pointcount = 0;
int linecount = 0;
int polycount = 0;
int rastercount = 0;
string subfields = "";
int subfieldcount = 0;
for (int i = 0; i < layercount; i++)
{
ILayer pLayer = pMap.get_Layer(i);
string name = pLayer.Name;
pointcount = pointcount + PointLayerTest(pLayer); //returns 1 if layer has point geometry
linecount = linecount + LineLayerTest(pLayer);
polycount = polycount + PolygonLayerTest(pLayer);
rastercount = rastercount + RasterLayerTest(pLayer);
if (pLayer is ICompositeLayer)
{
ICompositeLayer pCompLayer = pLayer as ICompositeLayer; //QI
int sublayern = pCompLayer.Count;
for (int j = 0; j < sublayern; j++) //go through group layer
{
ILayer pLayer2 = pCompLayer.get_Layer(j);
groupcount = groupcount + GroupLayerTest(pLayer2);
pointcount = pointcount + PointLayerTest(pLayer2);
linecount = linecount + LineLayerTest(pLayer2);
polycount = polycount + PolygonLayerTest(pLayer2);
rastercount = rastercount + RasterLayerTest(pLayer2);
IFeatureLayer2 pFeatureLayer = (IFeatureLayer2)pLayer2;
ITable subLayers = (ITable)pFeatureLayer;
subfieldcount = subLayers.Fields.FieldCount; //this line throws the error
subfields = subfields + Environment.NewLine + pLayer2.Name + ":" + subfieldcount;
}
}
}
//show all counts in pop-up window
MessageBox.Show("number of group layers:"+groupcount+ Environment.NewLine +"point layers:"+pointcount+
Environment.NewLine+"line layers:"+linecount+Environment.NewLine+"polygon layers:"+polycount+
Environment.NewLine+"raster layers:"+rastercount+Environment.NewLine+Environment.NewLine+subfields);
}
ERROR MESSAGE: An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in projectname.dll
Additional information: Error HRESULT E_FAIL has been returned from a call to a COM component.
1 Answer 1
It's difficult to tell not knowing what layers you are in your map, but this code: rastercount = rastercount + RasterLayerTest(pLayer2); makes it look like you are expecting an IRasterLayer.
I do not believe you can get an IFeatureLayer from a Raster layer. Try testing for IRasterLayer and skip over it.
-
1Also ILayer.Valid resources.arcgis.com/en/help/arcobjects-net/componenthelp/… - broken layers can mess up good code.. check the layer is valid before typeof(pLayer2) is IFeatureLayer or IStandaloneTable (both support IFields)Michael Stimson– Michael Stimson2016年10月13日 01:13:25 +00:00Commented Oct 13, 2016 at 1:13
Explore related questions
See similar questions with these tags.