[GIS] FeatureClass to IFeatureLayer

arcobjectsfeature-layer

I am trying to covert my feature class to feature layer so that i can pass "IQueryByLayer" over my feature class.

Till now i have my feature class —

FeatureClass className = new FeatureClass(cc);

want to do something like

IFeatureLayer newLayer = className.as---(FeatureLayer)

Can anybody point out a correct way to impliment it.

Best Answer

IFeatureLayer only make sense when your code is working within ArcMap. If that is your case, have a look at this code:

IFeatureLayer featureLayer = new FeatureLayerClass();
featureLayer.FeatureClass = featureWorkspace.OpenFeatureClass("2000_hrcn");
ILayer layer = (ILayer)featureLayer;
layer.Name = featureLayer.FeatureClass.AliasName;
// Add the layer to the focus map.
ESRI.ArcGIS.ArcMapUI.IMxDocument mxDocument = (ESRI.ArcGIS.ArcMapUI.IMxDocument)(m_application.Document);
IMap map = mxDocument.FocusMap;
map.AddLayer(layer);
//now you can use the the IFeatureLayer

Also note that A featureclass cannot be instantiated directly. You can only get it as a result from other methods like IfeatureWorkspace.OpenFeatureClass method.

Code like FeatureClass className = new FeatureClass(cc); will not compile