1

I'm trying to use the following code in an ArcMap 10.1 extension:

    private IFeatureLayer getLayer(string lname)
    {
        IMap          map;
        IFeatureLayer flayer;
        IEnumLayer    layerTag;
        ILayer        layer;

        try
        {
            map = ArcMap.Document.FocusMap;
            layerTag = map.Layers;
            layer = layerTag.Next();
            while (layer != null)
            {

                flayer = (IFeatureLayer) layer;

                if (flayer.Name.Equals(lname))
                {
                    if(validateFeatureClass(flayer.FeatureClass)){
                        flayer.Visible = true;
                        flayer.Selectable = true;
                        return flayer;
                    }
                }
                layer = layerTag.Next();
            }
            return null;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.ToString(), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            return null;
        }
    }

but it hits the skids (throws a cast exception) when I try to use flayer = (IFeatureLayer) layer;

I had before flayer = layer as IFeatureLayer but then name checking line failed due to a null reference exception.

Am I missing something obvious here?

2
  • You should stick with the flayer = layer as IFeatureLayer and then do the null check--it certainly is possible and likely, that you will have layers in your map document that don't implement IFeatureLayer (like a raster layer or image service layer). Dec 19, 2014 at 19:31
  • Are you checking that the layer is an IFeatureLayer? if (layer is IFeatureLayer) { flayer = (IFeatureLayer) layer; }
    – kenbuja
    Dec 19, 2014 at 19:50

2 Answers 2

3

IMap.Layers returns an enumerator over all layers in the map document. This includes group, raster, vector, graphics layers, and non-root layers in the TOC (i.e. it recurses into group layers). If you have any non feature layers in the map document, casting the ILayer to IFeatureLayer will throw an exception. To handle this, either do a dynamic cast and null check to skip non feature layers:

flayer = layer as IFeatureLayer;
if(layer == null)
{
    layer = layers.Next();
    continue;
}

Alternatively, fetch only the IFeatureLayers from the document:

private IFeatureLayer getLayer(string lname)
{
    IMap          map;
    IFeatureLayer flayer;
    IEnumLayer    layerTag;
    ILayer        layer;

    try
    {
        map = ArcMap.Document.FocusMap;

        //Create a UID for IFeatureLayer's interface ID
        UID uid = new UIDClass();
        uid.Value = "{" + typeof(IFeatureLayer).GUID.ToString() + "}";
        //Only fetch layers that implement that interface
        layerTag = map.get_Layers(uid);

        layer = layerTag.Next();
        while (layer != null)
        {

            flayer = (IFeatureLayer) layer;

            if (flayer.Name.Equals(lname))
            {
                if(validateFeatureClass(flayer.FeatureClass)){
                    flayer.Visible = true;
                    flayer.Selectable = true;
                    return flayer;
                }
            }
            layer = layerTag.Next();
        }
        return null;
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error: " + ex.ToString(), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        return null;
    }
}
1

Try using IEnumLayer interfase:

IEnumLayer pEnumLayer;

pEnumLayer = map.Layers;
layer = pEnumLayer.Next();
while (layer != null)
        {

            flayer = (IFeatureLayer) layer;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.