[GIS] How to determine the layer name in an edit session

add-inarcobjectseditingstandard-license-level

I have a problem with an editor extension in an Add-In for ArcGIS 10.
The editor extension listens on different events occuring on shapefile layers via registering listeners:

mEditEvents.OnChangeFeature += OnChangeFeature;
mEditEvents.OnCreateFeature += OnCreateFeature;
mEditEvents.OnDeleteFeature += OnDeleteFeature;

All these callbacks get are a parameter of type IObject, which can be cast to IFeature.
With this IFeature I can get the feature class and its alias name (IFeature.Class.AliasName) which seems to be the name of the shapefile associated with this feature, but I want the name of the layer associated with this feature which can be different from the shapefile's name.

Is there any way how to get this layer name?

Thanks in advance!

Update:

I found a solution (see my comment below) – thanks for the help!

Best Answer

You may need to get the dataset's name from the feature first. Then you can loop all the layers in TOC and check if their dataset name matches the one obtained earlier. It would look something like below.

IFeature feature = //get the feature..;
IDataset featureDs = (IDataset)feature.Class;
IMap map = //get the map...;

for (int i = 0; i < map.LayerCount; i++)
{
    ILayer layer = map.get_Layer(i);
    if (layer is IFeatureLayer)
    {
        IFeatureClass layerFc = ((IFeatureLayer)layer).FeatureClass;
        IDataset layerDs = (IDataset)layerFc;

        if (layerDs.Name == featureDs.Name)
        {
            //print the layer name in TOC..
            System.Diagnostics.Debug.Print(layer.Name);
        }
    }
}  
Related Question