Select to view content in your preferred language

IFeatureClassLoad cast exception

671
2
08-04-2011 03:44 PM
Labels (1)
MichaelMurphy8
Deactivated User
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();
            }
        }
0 Kudos
2 Replies
JenniferNery
Esri Regular Contributor
You might have posted in the wrong forum. There is no IFeatureClassLoad in ArcGIS API for WPF.
0 Kudos
MichaelMurphy8
Deactivated User
Yea.  I messed up.
0 Kudos