Select to view content in your preferred language

IDataLayer Example will not work in C#

563
0
07-13-2010 07:15 AM
CarlosPiccirillo
New Contributor III
Hi everyone,

I am trying to write some generic C# code that will identify the data source of any possible layer a user might add (Coverage, Shapefile, table, etc.). I thought I had the solution when I found the ESRI IDataLayer Example and this works well in VB6 or VBA. When I convert it to C# however, it fails for layers of type SDE Raster, Raster Catalog and File System Raster. pDatasetName becomes null with these data types and the code crashes.

So far in my testing I have determined that I can use IDataLayer2::DataSourceName::NameString for data types of: File System Raster, SDE Raster, ArcGIS Image Service, Raster Catalog, Coverage Feature Class and IDatasetName2::Name for data types of: Shapefile Feature Class, Personal Geodatabase Feature Class, File Geodatabase Feature Class. While this approach works, I do not want to have to write code for every possibility, i.e.

if (pLayer is IFeatureLayer)
{
    do something
}
else
if (pLayer is IRasterLayer)
{
    do something
}

            IMxDocument pMxDocument = (IMxDocument)m_application.Document;
            IMap pMap = pMxDocument.FocusMap;
            ILayer pLayer = pMap.get_Layer(0) as ILayer;
            IDataLayer2 pDataLayer = pLayer as IDataLayer2;
            IDatasetName2 pDatasetName = pDataLayer.DataSourceName as IDatasetName2;
            IWorkspaceName pWorkspaceName = pDatasetName.WorkspaceName;
            string name = string.Empty;

            if (pDatasetName is IFeatureClassName)
            {
                IFeatureClassName pFeatureClassName = pDatasetName as IFeatureClassName;

                if (pFeatureClassName.FeatureDatasetName != null)
                {
                    name = pFeatureClassName.FeatureDatasetName.Name;
                }
            }

            MessageBox.Show("Path: " + pWorkspaceName.PathName);
            MessageBox.Show("Feature Dataset: " + name);
            MessageBox.Show("Feature Class/Dataset: " + pDatasetName.Name);
0 Kudos
0 Replies