I am try to casting idataset to ifeaturelayer i did not get any idea

here my code

            ESRI.ArcGIS.ArcMapUI.IMxDocument mxd = ArcMap.Application.Document as
            ESRI.ArcGIS.Carto.IMap map = mxd.FocusMap;
            ESRI.ArcGIS.Geodatabase.IWorkspaceFactory wsf = new 
            ESRI.ArcGIS.Geodatabase.IWorkspace ws = wsf.OpenFromFile(@"D:\ARC OBJECTS\TemplateData.gdb", ArcMap.Application.hWnd);
            ESRI.ArcGIS.Geodatabase.IFeatureWorkspace fws = ws as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace;
            ESRI.ArcGIS.Geodatabase.IFeatureDataset fd = fws.OpenFeatureDataset("World");
  • 1
    You can't cast a IDataset to IFeatureLayer. You have to create a layer from that dataset first, then you can cast that layer to IFeatureLayer. Commented Jan 18, 2018 at 8:17
  • 1
    cag is correct. Be sure to look at the documentation to see which interfaces are shared on an object. For instance for a FeatureDataset class. You can see that it implements IDataset and a bunch of other interfaces. You can also click on IDataset and see all the classes that implements IDataset.
    – danielm
    Commented Jan 18, 2018 at 17:52
  • @cag that may be enough to write an answer if you include a link to the relevant documentation.
    – PolyGeo
    Commented Jan 19, 2018 at 4:46
  • @danielm that may be enough to write an answer.
    – PolyGeo
    Commented Jan 19, 2018 at 4:46
  • 1
    @PolyGeo, the OP is fairly on the right track until the last line where they go slightly astray... the difference between IDataset, IFeatureDataset and IFeatureClass are subtle but in this instance you can't get from IDataset to IFeatureLayer necessarily (the OP says IDataset but is really using IFeatureDataset) for the same sort of reason why you can't blindly cast ILayer to IFeatureLayer - it might work at runtime but isn't guaranteed. Commented Jan 19, 2018 at 4:54

1 Answer 1


I think you're barking up slightly the wrong tree here.. you're opening "World" as a FeatureDataset; if "World" is feature data, suitable to become a feature layer, then you want to open it as a FeatureClass using IFeatureWorkspace.OpenFeatureClass and not as a IFeatureDataset.

You can create a new IFeatureLayer and set the .FeatureClass property to the newly opened feature class to get an IFeatureLayer from an IFeatureClass:

IFeatureClass pFeatureClass = fws.OpenFeatureClass("World");
IFeatureLayer pFeatureLayer = new FeatureLayer();
pFeatureLayer.FeatureClass  = pFeatureClass;
pFeatureLayer.Name = "World";

This new feature layer will need to be added to the map if you want to see it and will have a default renderer (single symbol) applied but these are just refinements you can work on later.

  • Thank you, " world " is not a feature class it is a dataset name in that dataset 3 feature are there .Then how to we access as a feature class. I tried your suggestion its show the no table found. Commented Jan 19, 2018 at 5:07
  • 1
    Ah, if it actually is a dataset then you need to iterate the feature classes with IFeatureDataset.Subsets resources.arcgis.com/en/help/arcobjects-net/componenthelp/…. You have to create an IFeatureLayer for each feature class in the feature dataset to get the feature layers. This will return a real IDataset for each object in the feature dataset; check the IDataset.Type is FeatureClass before casting to IFeatureClass. Commented Jan 19, 2018 at 5:38

Your Answer

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

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