madgame

IFeatureClassLoad cast exception

Discussion created by madgame on Aug 5, 2011
Latest reply on Aug 5, 2011 by Neil
I've been trying to follow the example for using the IFeatureClassLoad interface for loading data from a file, and I keep getting a Cast Exception saying that the interface isn't supported.  Any ideas?

protected override void OnClick()
        {
            try
            {
                string filename = "C:\\Temp\\MyDataFile";
                ESRI.ArcGIS.esriSystem.IPropertySet propertySet = new ESRI.ArcGIS.esriSystem.PropertySetClass();
                propertySet.SetProperty("DATABASE", filename); 
                myApplication = Hook as IApplication;
                
                IWorkspaceFactory2 factory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
                IWorkspace2 workspace = factory.OpenFromFile(filename, 0) as IWorkspace2;

                IFeatureClass myFeature = CreateFeatureClass(workspace, null, "MyDataFile", null, null, null, "");
                
                if(myFeature != null) {
                    //Create an IFeatureLayer
                    IFeatureLayer featureLayer = new FeatureLayerClass();

                    CreateDummyPoint(myFeature);
                    featureLayer.FeatureClass = myFeature;
                    ILayer layer = (ILayer)featureLayer;
                    layer.Name = featureLayer.FeatureClass.AliasName;
                    IMxDocument mxDocument = (IMxDocument)myApplication.Document;
                    IMap map = mxDocument.FocusMap;
                    map.AddLayer(layer);
                }
            }
            catch (NullReferenceException nre) {
                Console.WriteLine("error", nre);
            } catch (Exception e) {
                Console.WriteLine("Uh Oh", e);
            } finally {
                Console.WriteLine("Done");
            }
        }

        private void CreateDummyPoint(IFeatureClass featureClass) {
            string anotherFile = "c:\\Temp\\SampleTextData.txt";
            int[] fields = new int[2];
            fields[0] = 20;
            fields[1] = 20;

            IFeatureClassLoad featureClassLoad = (IFeatureClassLoad)featureClass; -- HERE'S WHERE IT FAILS!!!

            TextFieldParser parser = new TextFieldParser(anotherFile);
            parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.FixedWidth;
            parser.SetFieldWidths(fields);

            while(!parser.EndOfData) {
                string[] strFields = parser.ReadFields();
                IFeature feature = featureClass.CreateFeature();
                IPoint point = new PointClass();
                point.X = Convert.ToDouble(strFields[0]);
                point.Y = Convert.ToDouble(strFields[1]);
                feature.Shape = point;
                feature.Store();
            }
        }

Outcomes