Method to get IFeatureClass

1810
6
Jump to solution
02-14-2013 06:42 AM
RyanHawkins
New Contributor II
I am trying to create a method where I am able to select a specific layer and return the IFeatureClass of that layer. I used a return type double and it returns the correct value: 50. I would normally have return featureclass; but I am using the double to test the logic. I am unsure of when I would normally return 0, how I return an IFeatureClass. I am going to use this method to select two different featureclasses for spatial queries. Any suggestions from how to solve this problem to critiques on my coding techniques will be greatly appreciated! Below is the code I currently have:

public double GetFeatureLayer(string layerName)
{
try
{
IMap map = ArcMap.Document.FocusMap;

//Bail if map has no layers.
if (map.LayerCount == 0)
{
MessageBox.Show("There are no layers importerd. Please add Layers and try again");
return 0;
}

//Fetch all the feauture layers in the focus map
//to determine if at least one is selectable
IUID uid = new UIDClass();
uid.Value = "{40A9E885-5533-11D0-98BE-00805F7CED21}";
IEnumLayer enumLayer = map.get_Layers((UID)uid, true);
enumLayer.Reset();
IFeatureLayer featureLayer = (IFeatureLayer)enumLayer.Next();

while (featureLayer != null)
{
if (featureLayer.Name.ToString() == "Structure")
{
IFeatureClass featureClass = featureLayer.FeatureClass;
return 50;
}

else
{
featureLayer = (IFeatureLayer)enumLayer.Next();
}
}
return 0;
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace);
return 0;
}
}
0 Kudos
1 Solution

Accepted Solutions
RyanHawkins
New Contributor II
No worries! I think I figured it out. It might not be the most efficient coding but here is what I came up with!

        public IFeatureLayer GetFeatureLayer(string layerName)
        {
            try
            {
                IMap map = ArcMap.Document.FocusMap;

                //Bail if map has no layers.
                if (map.LayerCount == 0)
                {
                    MessageBox.Show("There are no layers importerd. Please add Layers and try again");
                    IFeatureLayer layer = null;
                    return layer;
                }

                //Fetch all the feauture layers in the focus map
                //to determine if at least one is selectable
                IUID uid = new UIDClass();
                uid.Value = "{40A9E885-5533-11D0-98BE-00805F7CED21}";
                IEnumLayer enumLayer = map.get_Layers((UID)uid, true);
                enumLayer.Reset();
                IFeatureLayer featureLayer = (IFeatureLayer)enumLayer.Next();

                while (featureLayer != null)
                {
                    if (featureLayer.Name.ToString() == layerName)
                    {
                        return featureLayer;
                    }

                    else
                    {
                        featureLayer = (IFeatureLayer)enumLayer.Next();
                    }
                }
                IFeatureLayer layer2 = null;
                return layer2;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
                IFeatureLayer layer = null;
                return layer;
            }
        }

View solution in original post

0 Kudos
6 Replies
LeoDonahue
Occasional Contributor III
ESRI already created a sample method for getting a layer by name. 

It looks like this:
map is a reference to an IMap variable.

    /**
     * ESRI sample method to get feature class layer by name, instead of index.
     * @param layerName the layer name as String. 
     * @return layer as <code>FeatureLayer</code>.
     */
    public static FeatureLayer getLayerByName(String layerName) {
        FeatureLayer layer = null;
        try {
            for (int i = 0; i < map.getLayerCount(); i++) {
                if (map.getLayer(i).getName().equalsIgnoreCase(layerName)) {
                    layer = (FeatureLayer) map.getLayer(i);
                    break;
                }
            }
        } catch (AutomationException ae) {
            ae.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Returns the featurelayer
        return layer;
    }
0 Kudos
RyanHawkins
New Contributor II
Perfect thank you! I am getting errors involving the if statement, if (map.getLayer(i).getName().equalsIgnoreCase(layerName)). I am getting errors:
'ESRI.ArcGIS.Carto.ILayer.Name.get': cannot explicitly call operator or accessor error
'ESRI.ArcGIS.Carto.IMap' does not contain a definition for 'getLayer" and no extension method 'getLayer' accepting a first argument of type 'ESRI.ArcGIS.Carto.IMap' could be found.

I can get map.get_Layer(i).Name.Equals(layerName); but I am still getting the error  'ESRI.ArcGIS.Carto.ILayer.Name.get': cannot explicitly call operator or accessor. I was wondering how to get around this?

Also, I was wondering what reference you use for AutomationException. I have been looking around on the internet but am failing to find it.
0 Kudos
LeoDonahue
Occasional Contributor III
Sorry Ryan, my sample was a Java sample.
0 Kudos
RyanHawkins
New Contributor II
No worries! I think I figured it out. It might not be the most efficient coding but here is what I came up with!

        public IFeatureLayer GetFeatureLayer(string layerName)
        {
            try
            {
                IMap map = ArcMap.Document.FocusMap;

                //Bail if map has no layers.
                if (map.LayerCount == 0)
                {
                    MessageBox.Show("There are no layers importerd. Please add Layers and try again");
                    IFeatureLayer layer = null;
                    return layer;
                }

                //Fetch all the feauture layers in the focus map
                //to determine if at least one is selectable
                IUID uid = new UIDClass();
                uid.Value = "{40A9E885-5533-11D0-98BE-00805F7CED21}";
                IEnumLayer enumLayer = map.get_Layers((UID)uid, true);
                enumLayer.Reset();
                IFeatureLayer featureLayer = (IFeatureLayer)enumLayer.Next();

                while (featureLayer != null)
                {
                    if (featureLayer.Name.ToString() == layerName)
                    {
                        return featureLayer;
                    }

                    else
                    {
                        featureLayer = (IFeatureLayer)enumLayer.Next();
                    }
                }
                IFeatureLayer layer2 = null;
                return layer2;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace);
                IFeatureLayer layer = null;
                return layer;
            }
        }
0 Kudos
MarcinDruzgala
Occasional Contributor
No worries! I think I figured it out. It might not be the most efficient coding but here is what I came up with! 
if (map.LayerCount == 0)
{
 MessageBox.Show("There are no layers importerd. Please add Layers and try again");
 IFeatureLayer layer = null;
 return layer;        (1)     
}

while (featureLayer != null)
{
 if (featureLayer.Name.ToString()   (2) == layerName)
 {
  return featureLayer;
 }

 else
 {
  featureLayer = (IFeatureLayer)enumLayer.Next();
 }
}
IFeatureLayer layer2 = null;
return layer2; (3) 



Hi Ryan just got few tips for you(I've colored the code)
1. and 3. No need for creating a layer if you want to pass a null value, if you want your method to return null just write "return null;"
2. featureLayer.Name returns you a string value so no need for using a ToString() method 😉

Cheers
MDruzgala
0 Kudos
RyanHawkins
New Contributor II
MDruzgala,

Thanks for the tips! Those little things I'm still learning.
0 Kudos