0

Would anyone know the reason why this happens? and possibly a way to fix it. I am stumped

In the following code. The first addlayer works, but crashes on the second addlayer with a 'vshost32.exe has stopped working' error.

However when I comment out all the addlayers and make it to the for loop. All three addlayers display correctly.

Why would the exact same code work in a for loop, but not when add a second, third and fourth layer individually.

Any ideas?


I have taken care of the error, however when I stop the program, I loose the data in Arcmap 10

// want to open shapefile
pFeatureClass = pLocalFeatureWorkspace.OpenFeatureClass("wa_body_" + sGeoname);
pFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)pObjectFactory.Create("esriCarto.FeatureLayer");

pFeatureLayer.FeatureClass = pFeatureClass;
pDataset = (ESRI.ArcGIS.Geodatabase.IDataset)pFeatureClass;
pFeatureLayer.Name = pDataset.Name;

pMap.AddLayer(pFeatureLayer);

pFeatureClass = pLocalFeatureWorkspace.OpenFeatureClass(QCForm.chkLstNSHNPolygons.Items[8].ToString()); // "wa_poly_" + sGeoname);
pFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)pObjectFactory.Create("esriCarto.FeatureLayer");
pFeatureLayer.FeatureClass = pFeatureClass;
pDataset = (ESRI.ArcGIS.Geodatabase.IDataset)pFeatureClass;
pFeatureLayer.Name = pDataset.Name;

pMap.AddLayer(pFeatureLayer);

pFeatureClass = pLocalFeatureWorkspace.OpenFeatureClass(QCForm.chkLstNSHNLines.Items[4].ToString());// "nshn_line_" + sGeoname);
pFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)pObjectFactory.Create("esriCarto.FeatureLayer");
pFeatureLayer.FeatureClass = pFeatureClass;
pDataset = (ESRI.ArcGIS.Geodatabase.IDataset)pFeatureClass;
pFeatureLayer.Name = pDataset.Name;

pMap.AddLayer(pFeatureLayer);

pFeatureClass = pLocalFeatureWorkspace.OpenFeatureClass(QCForm.chkLstNSHNPoints.Items[5].ToString()); // "nshn_point_" + sGeoname);
pFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)pObjectFactory.Create("esriCarto.FeatureLayer");
pFeatureLayer.FeatureClass = pFeatureClass;
pDataset = (ESRI.ArcGIS.Geodatabase.IDataset)pFeatureClass;
pFeatureLayer.Name = pDataset.Name;

pMap.AddLayer(pFeatureLayer);

pFeatureClass = pLocalFeatureWorkspace.OpenFeatureClass(QCForm.chkLstNSHNJunctions.Items[4].ToString()); // "nshn_junc_" + sGeoname);
pFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)pObjectFactory.Create("esriCarto.FeatureLayer");
 pFeatureLayer.FeatureClass = pFeatureClass;
 pDataset = (ESRI.ArcGIS.Geodatabase.IDataset)pFeatureClass;
 pFeatureLayer.Name = pDataset.Name;

 pMap.AddLayer(pFeatureLayer);

 //Add topology layers
 i = 0;

 for (i = 0; i <= lstFeatures.Items.Count - 1; i++)
 {
     pFeatureClass = pLocalFeatureWorkspace.OpenFeatureClass(lstFeatures.Items[i].ToString());
     pFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)pObjectFactory.Create("esriCarto.FeatureLayer");
     pFeatureLayer.FeatureClass = pFeatureClass;
     pDataset = (ESRI.ArcGIS.Geodatabase.IDataset)pFeatureClass;
     pFeatureLayer.Name = pDataset.Name;

     pMap.AddLayer(pFeatureLayer);
 }

}

2
  • What is pObjectFactory set to? Dec 1 '15 at 20:43
  • pObjectFactory = (ESRI.ArcGIS.Framework.IObjectFactory)m_pApplication;
    – Deke
    Dec 2 '15 at 11:54
1

Working:

private void btnViewInArcMap_Click(object sender, EventArgs e)
{
    pLocalWorkspaceFactory = new ShapefileWorkspaceFactoryClass();

    string sGeoname = null;

    pObjectFactory = (ESRI.ArcGIS.Framework.IObjectFactory)m_pApplication;

    IBasicDocument document = (IBasicDocument)m_pApplication.Document;

    pMxDocument = (ESRI.ArcGIS.ArcMapUI.IMxDocument)m_Document;
    pMap = pMxDocument.FocusMap;

    switch (clsGlobal.ConnectType)
    {
        case clsGlobal.ConnectionTypeEnum.SHP:
            //Use reflection to get ClsID of ShapefileWorkspaceFactory.
            Type shpWkspFactType = typeof(ShapefileWorkspaceFactoryClass);

            string typeClsID = shpWkspFactType.GUID.ToString("B");

            pLocalWorkspaceFactory = (IWorkspaceFactory)pObjectFactory.Create(typeClsID);
            break;
    }

    pLocalFeatureWorkspace = (IFeatureWorkspace)pLocalWorkspaceFactory.OpenFromFile(clsGlobal.pWorkspace.PathName, 0);

    // want to open shapefile
    pFeatureClass = pLocalFeatureWorkspace.OpenFeatureClass("wa_body_" + sGeoname);
    // Create the layer
    pFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)pObjectFactory.Create("esriCarto.FeatureLayer");
    pFeatureLayer.FeatureClass = pFeatureClass;
    pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;

    document.AddLayer(pFeatureLayer);

    pFeatureClass = pLocalFeatureWorkspace.OpenFeatureClass("wa_poly_" + sGeoname);
    // Create the layer
    pFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)pObjectFactory.Create("esriCarto.FeatureLayer");
    pFeatureLayer.FeatureClass = pFeatureClass;
    pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;

    document.AddLayer(pFeatureLayer);

    pFeatureClass = pLocalFeatureWorkspace.OpenFeatureClass("nshn_line_" + sGeoname);
    // Create the layer
    pFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)pObjectFactory.Create("esriCarto.FeatureLayer");
    pFeatureLayer.FeatureClass = pFeatureClass;
    pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;

    document.AddLayer(pFeatureLayer);

    pFeatureClass = pLocalFeatureWorkspace.OpenFeatureClass("nshn_point_" + sGeoname);
    // Create the layer
    pFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)pObjectFactory.Create("esriCarto.FeatureLayer");
    pFeatureLayer.FeatureClass = pFeatureClass;
    pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;

    document.AddLayer(pFeatureLayer);

    pFeatureClass = pLocalFeatureWorkspace.OpenFeatureClass("nshn_junc_" + sGeoname);
    // Create the layer
    pFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)pObjectFactory.Create("esriCarto.FeatureLayer");
    pFeatureLayer.FeatureClass = pFeatureClass;
    pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;

    document.AddLayer(pFeatureLayer);

    //Add topology layers
    i = 0;

    for (i = 0; i <= lstFeatures.Items.Count - 1; i++)
    {
        pFeatureClass = pLocalFeatureWorkspace.OpenFeatureClass(lstFeatures.Items[i].ToString());
        // Create the layer
        pFeatureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)pObjectFactory.Create("esriCarto.FeatureLayer");
        pFeatureLayer.FeatureClass = pFeatureClass;
        pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;

        document.AddLayer(pFeatureLayer);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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