3

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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Oct 6, 2016 at 16:33

1 Answer 1

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.

answered Oct 6, 2016 at 19:33
1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.